Browsing 7239 questions and answers with Jon Skeet

Java getResourceAsStream static workaround. Correct?

I was using this code to get my resources: this.getClass().getResourceAsStream("Image.png"); it worked fine up until the point where I had to implement it in a static...
Jon Skeet
people
quotationmark

Use a class literal instead. It's not clear what AudioLoop is in this case, but unless it's a class in your own library, that's probably not what you want. I suspect you want something like: new Image(display,... more 1/29/2014 5:23:44 PM

people

method to call other methods and handle exceptions

Background: I wrote a C# application that is calling multiple methods from a web service. Methods are being called from loops in Main, from some classes, and so on. Each of these...
Jon Skeet
people
quotationmark

It sounds like you probably want something like this: private T CallWithRetries<T>(Func<T> call) { // TODO: Work out number of retries, etc. for (int i = 0; i < 3; i++) { try { return... more 1/29/2014 4:43:45 PM

people

Why is a static field used for a singleton?

I've been reading up on singletons recently and I commonly see code like: private static SingletonDemo instance = null; Inside the Singleton class as a field. I don't want...
Jon Skeet
people
quotationmark

The point is that you need a way of accessing the single instance which has been created - it's effectively a global variable. That's precisely what a static field achieves. (At least, it's global as far as the relevant classloader is... more 1/29/2014 4:31:17 PM

people

Is it possible to create C# client to connect to a specific JVM based server (Netty)?

I am working on a C# client for a server that wraps Netty. It is a TCP/IP server and I have tried using C# class TcpClient, but could not write anything onto the server or receive...
Jon Skeet
people
quotationmark

Judging by the code sample you've given, it looks like the data is encoded using Protocol Buffers, Google's serialization format. Fortunately, there are at least two libraries implementing Protocol Buffers for .NET: protobuf-net:... more 1/29/2014 4:28:58 PM

people

Convert String to Int (NOT PARSE)

How can i get the numeric representation of a string in C#? To be clear, I do not want the address of the pointer, I do not want to parse an int from a string, I want the numeric...
Jon Skeet
people
quotationmark

For the purposes of GetHashCode, you should absolutely call GetHashCode. However, to answer the question as asked (after clarification in comments) here are two options, returning BigInteger (as otherwise you'd only get two characters in... more 1/29/2014 3:06:48 PM

people

Overriding a method which uses a subtype

I have an abstract class having an abstract method: public abstract SomeClass Do(SomeClass o1, SomeClass o2); Then when I implement in a concrete class which extends that...
Jon Skeet
people
quotationmark

You get an error because it doesn't support the same interface. Imagine a caller like this: AbstractClass x = new ImplementingClass(); x.Do(new SomeClass(), new SomeClass()); That should work - it's just using the AbstractClass abstract... more 1/29/2014 12:21:08 AM

people

C# Threading/Async: Running a task in the background while UI is interactable

After looking around on both Async/Await and Threading, I'm still unsure of the right way to apply it to my situation. No matter the variation that I try my UI still hangs because...
Jon Skeet
people
quotationmark

You've definitely implemented it incorrectly. You're returning a Task<int>, but only once all the work has already been done. It seems to me that you should probably just have a synchronous method: private static void... more 1/28/2014 11:42:23 PM

people

Why does (int)(1.0 / x) where x = 0 results in In32.MinValue rather than Int32.MaxValue?

In Java, int x = 0; (int)(-1.0 / x) -> Integer.MinValue (int)(1.0 / x) -> Integer.MaxValue But in C#, int x = 0; (int)(-1.0 / x) -> Int32.MinValue (int)(1.0 / x)...
Jon Skeet
people
quotationmark

One shouldn't expect anything, really. From the C# specification, section 6.2.1 (emphasis mine): For a conversion from float or double to an integral type [...]. - In an unchecked context, the conversion always succeeds, and proceeds... more 1/28/2014 9:47:18 PM

people

Java Map synchronization

I'm working on something where I have a Collections.synchronizedMap defined. In one thread A, it will just get the value, and put an updated value so it is not a problem. In...
Jon Skeet
people
quotationmark

It sounds like you just need to synchronize the "fetch, check and delete" part. For example: Iterable<String> keys; synchronized (map) { keys = new ArrayList<>(map.keySet()); } for (String key : keys) { synchronized... more 1/28/2014 9:08:28 PM

people

How to multi use switch statement?

I have two scenarios that use the same switch structure. Rather than creating two switches that are basically duplicates, is there a way to reuse the structure? First...
Jon Skeet
people
quotationmark

Well, you could have: private static readonly Dictionary<string, Func<Foo>> SomeSensibleName = new Dictionary<string, Func<Foo>> { { Constant1, () => new Class1() }, { Constant2, () => new... more 1/28/2014 6:53:58 PM

people