Browsing 7239 questions and answers with Jon Skeet

Why nested classes behave differently between Java and C#?

I don't understand why the following code errors in Java: public abstract class TestClass { private final int data; protected TestClass(int data) { this.data = data; } ...
Jon Skeet
people
quotationmark

To create an inner class (as opposed to a nested static class) you need an instance of the enclosing class - you don't have one in this case. Note that there's no direct equivalent of inner classes in C# - a nested class in C# is more like... more 3/19/2015 6:44:00 AM

people

Having T type how to find all classes that expand Base<T> in assembly?

Say I have: public class Base<T> {} public class MyBaseOne : Base<int> {} public class MyBaseTwo : Base<int> {} Having T type (say int) how to find all...
Jon Skeet
people
quotationmark

You can use Type.IsAssignableFrom: var typeArgument = typeof(int); var baseType = typeof(Base<>).MakeGenericType(typeArgument); var matches = someAssembly.GetTypes() .Where(t =>... more 3/18/2015 10:19:33 PM

people

Transform XmlNode into XmlReader

I wonder what is the best way to turn a XmlNode object into an XmlReader... I could even name a few ways to do this ... But they use a MemoryStream to make the...
Jon Skeet
people
quotationmark

Just use the XmlNodeReader constructor: using (XmlReader reader = new XmlNodeReader(content)) { // ... } (The documentation says you should use XmlReader.Create - but there are no overloads taking an XmlNode, so that doesn't seem... more 3/18/2015 9:57:30 PM

people

Access local variables in superclass constructor

I have a super class that has this constructor: public Super(String p){ String[] result = p.split(","); setA(result[0]); setB(result[1]); setC(result[2]); ...
Jon Skeet
people
quotationmark

Basically you'd need to do the split again in the subclass constructor - the local variable result isn't available in the subclass constructor: public Sub(String d){ super(d); String[] result = d.split(","); setF(result[5]); ... more 3/18/2015 7:47:20 PM

people

Two dates as properties in C#

I want to have two simple properties: the start date and the end date. I want to put a constraint that the start date must be before the end date. The problem arises when...
Jon Skeet
people
quotationmark

Well, you might think about whether it would make sense to have a type which represents a start/end combination. A sort of... Interval. (Yes, this is a not-so-subtle plug for Noda Time, which makes date/time handling generally better... more 3/18/2015 7:42:45 PM

people

How to create an int named "int" in Java

Well, as title says it is a weird question. I want to create an int called int and of course if i write; int int; java(eclipse) gives error. So I've been wondering if it is...
Jon Skeet
people
quotationmark

You can't. int isn't a valid identifier in Java, because it's a keyword. From JLS 3.8: An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal... more 3/18/2015 7:12:29 PM

people

Error parsing data org.json.JSONException , already try other post

I know there are several posts with the same error, but I've proven solutions and still have the same error Log.txt: 03-18 18:32:33.082: D/gralloc_goldfish(974): Emulator...
Jon Skeet
people
quotationmark

You're parsing an empty string, because you never assign a different value to json. You make a request, and set is to refer to the input stream from the response - but you then don't use is. You might want: JObj = new JSONObject(new... more 3/18/2015 7:04:11 PM

people

Using .NET shims to unit test datetime method

I have to test a method that calculates the time difference of the local time and UTC. So I've tried to use a shims in order to change the local time and UTC but this does not...
Jon Skeet
people
quotationmark

You're calling DateTime.Now before you set up the shim... so it can't possibly retroactively change the value of actual. I suspect if you use: Dim actual As DateTime 'Act Using (ShimsContext.Create()) System.Fakes.ShimDateTime.NowGet... more 3/18/2015 4:10:25 PM

people

Run a Task, do something and then wait?

I am familiar with the basic of Tasks, asyn/await etc but I haven't done much advance stuff and I a little stuck on an issue. I have a method which does communication to a camera...
Jon Skeet
people
quotationmark

Sounds like you just want to await later in the method: public async void SomeMethod() { var cameraTask = DoCameraWork(); TextBox.Text = "Waiting For camera work"; SendData(); var result = await cameraTask; TextBox.Text =... more 3/18/2015 3:25:38 PM

people

C# are value types in a delegate different to those in the parent method

Consider the below block of code: int noThreads = noProcessors - 1; var countdownEvent = new CountdownEvent(noThreads); if (noThreads == 0) noThreads = 1; float blockSize =...
Jon Skeet
people
quotationmark

I want to know whether the variables startIndex and stopIndex within the delegate point to the same location as their namesakes outside the delegate. Yes, they do. The variables themselves are captured - changes made within the... more 3/18/2015 2:53:00 PM

people