Browsing 7239 questions and answers with Jon Skeet

Use of indexers on properties in a customer class

Been google.ing for some time now and nothing seems to match my specific issue. I have created my own class with some properties such as public class cPerson { public int? iID...
Jon Skeet
people
quotationmark

Firstly, this seems like a generally bad idea. If you find yourself really needing this, you should carefully consider design alternatives. Secondly, it's not clear that you can get the properties in declaration order - I would strongly... more 6/19/2014 8:22:31 AM

people

Call constructor in an abstract class

Is it possible to call a constructor in a abstract class? I read that this constructor can be called through one of its non-abstract subclasses. But I don't understand that...
Jon Skeet
people
quotationmark

You can't call an abstract class constructor with a class instance creation expression, i.e. // Invalid AbstractClass x = new AbstractClass(...); However, in constructing an object you always go through the constructors of the whole... more 6/19/2014 7:15:15 AM

people

What is the right direction of using "*.isInstance"?

I am confused every time I read the Java Documentation again to that. So please try to help me in your own words. List<Parent> list = new ArrayList<Parent>(); //Child...
Jon Skeet
people
quotationmark

That's not checking what you want to check either way round. You want: if (Child.class.isInstance(p)) That's equivalent to: if (p instanceof Child) ... except you can specify the class to check dynamically, rather than it being fixed... more 6/19/2014 6:27:05 AM

people

Why does initialising a derived class variable and assigning to its base class type not allow access to its members?

I have started learning C# and object oriented programming and I must admit it's really fun. Now, as I was learning about inheritance and polymorphism, the following thought...
Jon Skeet
people
quotationmark

Your self-answer in the first bullet point is correct: the compiler doesn't look at what values have been assigned to a variable when working out what members are available. It only cares about the compile-time type of the variable. It's... more 6/19/2014 6:19:16 AM

people

Why is a round trip conversion via a string not safe for a double?

Recently I have had to serialize a double into text, and then get it back. The value seems to not be equivalent: double d1 = 0.84551240822557006; string s =...
Jon Skeet
people
quotationmark

It seems to me that this is simply a bug. Your expectations are entirely reasonable. I've reproduced it using .NET 4.5.1 (x64), running the following console app which uses my DoubleConverter class.DoubleConverter.ToExactString shows the... more 6/19/2014 6:09:38 AM

people

Android error when convert month name

With this query I select the month in the database. The cursor returns the exact month, but when I do the conversion to display the full name appears the following month. For...
Jon Skeet
people
quotationmark

For example, the cursor returns 6, instead the method returns getDisplayName July. Yes, it would. Because the Calendar.MONTH "logical field" is 0-based. (Crazy decision, but...) Basically you need to subtract 1 from the month... more 6/18/2014 8:18:16 PM

people

Does C# ?? operator get called twice?

Does the method on the left side of the ?? operator in C# get called twice? Once for the evaluation and once for the assignment? In the following line: int i = GetNullableInt()...
Jon Skeet
people
quotationmark

There is a bug in the current C# compiler which will cause some aspects of evaluating the first operand to occur twice, in very specific situatoins - but no, GetNullableInt() will only be called once. (And the bug has been fixed in... more 6/18/2014 7:49:19 PM

people

Why does << 32 not result in 0 in javascript?

This is false: (0xffffffff << 31 << 1) === (0xffffffff << 32) It seems like it should be true. Adding >>> 0 anywhere does not change this. Why is...
Jon Skeet
people
quotationmark

The shift operators always effectively has a right operand in the range 0-31. From the Mozilla docs: Shift operators convert their operands to 32-bit integers in big-endian order and return a result of the same type as the left... more 6/18/2014 2:26:18 PM

people

LINQ to XML filter descendants C#

I have xml that is sent by a third party and I want to validate it. XElement xTree = XElement.Parse(@"<Container> <TrackingReferences> ...
Jon Skeet
people
quotationmark

Okay, it sounds like you want the TrackingCode elements rather than the TrackingReference elements, so it's actually pretty easy: var query = doc.Descendants("TrackingReference") // TODO: Filter based on TrackingName if you... more 6/18/2014 1:02:37 PM

people

Passing scanner to method

Can anybody tell me why the date prints 0/0/0? What part of the code is missing so the values input using Scanner are passed into the method DisplayDate and printed on the screen?...
Jon Skeet
people
quotationmark

This is the problem: public Date(int day, int month, int year) { } Your constructor completely ignores its parameters, leaving your fields with their default values. For int fields, that's 0. Instead, it should save the values,... more 6/18/2014 12:52:51 PM

people