Browsing 7239 questions and answers with Jon Skeet

child class constructor cannot assign to readonly variable inside

I have this parent class: public class ParentSchedule { protected readonly int[] numbersArray; public ParentSchedule() { numbersArray = new int[48]; ...
Jon Skeet
people
quotationmark

Is there a way for me to write to the readonly field from the child class? No. A readonly field can only be assigned in the constructor of the type that declares it. Perhaps you should make a protected constructor in the base class... more 3/23/2015 4:14:14 PM

people

Generics cannot convert type

interface IComponent { /*code*/ } interface IContent : IComponent { /*code*/ } interface IMedia : IComponent { /*code*/ } class A : IContent { /*code*/ } class B : IMedia {...
Jon Skeet
people
quotationmark

You need to cast to T - but first, somewhat annoyingly, you need to cast to object due to the rules around conversions to type parameters in C#. I would personally get rid of the local variable though, which isn't helping you: private... more 3/23/2015 4:02:28 PM

people

c# Reading XML with XElement

When I run the following code and step through it with a break point and look at temp I see "Empty, Enumeration yielded no results" and the MessageBox.Show never fires. I'm trying...
Jon Skeet
people
quotationmark

You're looking for elements called Season directly under the root element... whereas your XML looks like this: <Show> <name>The X-Files</name> <totalseasons>9</totalseasons> <Episodelist> ... more 3/23/2015 1:55:12 PM

people

BufferedReader chained to socket, how does readLine() != null work?

I am a bit confused as to what is going on here. Going through Head First Java and I am making a multithreaded server. The part I don't understand is here, where reader is a...
Jon Skeet
people
quotationmark

My question is, how does it detect that there is something in the inputstream, how does it know when it becomes null? This is more to do with the InputStream from socket.getInputStream() than BufferedReader, really. Any call to... more 3/23/2015 1:08:10 PM

people

Java get the date X days later

I am setting up a db40 database, and it is filled with instances of my class Day() which has sales information for that day in history, and an integer for its ID. The id is...
Jon Skeet
people
quotationmark

As you're using Java 8, I'd use java.time for this: Convert the integer (ick) into a LocalDate Add however many days you want Convert back to an integer (ick) So something like: static LocalDate dateFromId(int id) { int year = id... more 3/22/2015 4:45:10 PM

people

Index and length must refer to a location within the string

I have some address like this 122/852 Mansrovar society,Jaipur, Rajastha,India where i need to write a function which extract Mansrovar society I tried below code but...
Jon Skeet
people
quotationmark

The second argument of string.Substring(int, int) is the length of the substring you want - so BuildingAddress = BuildingAddress.Substring(BuildingAddress.IndexOf(' '), ... more 3/22/2015 4:32:02 PM

people

How to get properties from an object without initializing or constructing the object

It don't sound like something that should be possible, but I'd like to ask prior to writing it off. I am writing an app with a plugin system, I would like to get the properties...
Jon Skeet
people
quotationmark

It sounds like your plugins should be configured via attributes rather than via exposing properties: [Plugin(Name = "My Plugin", SafeName = "MyPlugin" ...)] public class InputMonitor You can access the attributes via reflection without... more 3/22/2015 12:43:47 PM

people

why we cannot access static members from instance object?

I know that I cannot call static method from instance object For example public class A { public static int a; } A b = new A(); b.a = 5; //which cannot compile I would like...
Jon Skeet
people
quotationmark

Because it makes no sense, and leads to misleading code. When reading the code, it gives the impression that a is part of the instance referred to by b. For example, consider: ClassA a1 = new ClassA(); ClassA a2 = new ClassA(); a1.a =... more 3/22/2015 9:40:38 AM

people

Why am I able to access enum constants directly as case labels of a switch statement in another class

Why am I able to access enum constant HOT directly in the code below: enum SPICE_DEGREE { MILD, MEDIUM, HOT, SUICIDE; } class SwitchingFun { public static void...
Jon Skeet
people
quotationmark

It's just the way the language is defined - when case labels are enum values, that means the switch expression must be an enum of that type, so specifying the enum type everywhere would be redundant. From JLS 14.11: Every case label... more 3/21/2015 10:43:39 AM

people

Why does visual studio give me this error type expected

I'm Developing windows phone 8.1 application when i want to start emulator Give me this error "Type expected" line 27 this is the line of code: statusBar.BackgroundColor = new...
Jon Skeet
people
quotationmark

You're trying to use the new operator, which requires a type, like this: variable = new SomeType(constructorArguments); You've got new and then a cast. I suspect you just want the cast, without new: // With a using directive for... more 3/21/2015 8:36:47 AM

people