Browsing 7239 questions and answers with Jon Skeet

How to pass the current instance to FieldInfo.GetValue inside the instance?

I'm making an abstract class that I need to validate fields in it's descendants. All the validated fields are marked by an attribute and are suppose to be checked for nulls or...
Jon Skeet
people
quotationmark

As Lucas says, the problem will be calling ToString() when you shouldn't. Presumably your attribute should only be applied to string fields anyway, so the simplest approach is just to cast the result to string. If that cast fails, it... more 1/28/2015 10:35:36 AM

people

String.split() method order of token in the returned String array

I am working on a application where I have to deal with a custom variable aVar- String aVar = price/barcode/area1/true // varName/varType/varScope/tracable There may be some...
Jon Skeet
people
quotationmark

Assuming you're actually calling String.split(String), that method's documentation includes: This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty... more 1/28/2015 7:10:19 AM

people

Convert String to Byte Array without Encoding?

So I have a string with Binary Data like this: String lob = "ÿØÿà..."; I really have no control over this so I have to take it as it is. So I need to convert this as an...
Jon Skeet
people
quotationmark

There's no such concept as a conversion like that without encoding. You're converting between characters and bytes - those aren't the same thing, so a conversion is required, and the form of the conversion is precisely the encoding.... more 1/27/2015 8:36:15 PM

people

Can the Elapsed callback of a System.Timers.Timer be async?

Is it possible (or even reasonable) to make the callback of a System.Timers.Timer an async method? Something like: var timer = new System.Timers.Timer { Interval =...
Jon Skeet
people
quotationmark

Will the timer await the callback before resetting the timer? No. There's nothing it could await, because the signature of ElapsedEventHandler has a void return type. In other words, your code is equivalent to: var timer = new... more 1/27/2015 6:16:37 PM

people

TCPClient.Read Reading in chunks of as low as 4 bytes

I fully understand the fact that I cannot read the max allotted if the data has not arrived yet. But is it really reasonable to read at a speed of 4 bytes? I mean if I am...
Jon Skeet
people
quotationmark

This is at least part of the problem: rLength = stream.Read(buf, 0, buf.Length); ... if (rLength < buf.Length) { byte[] temp = new byte[rLength]; Array.Copy(buf, temp, rLength); buf = temp; } Basically you're limiting... more 1/27/2015 5:22:37 PM

people

In java we can derived the class from abstract class in function itself. Can we do for C# also?

In Java we can derive the class from abstract class in function itself. Can we do the same thing for C#? public class A { public final static A d = new A(); protected...
Jon Skeet
people
quotationmark

No, there's no equivalent of anonymous inner classes in C#. Typically for single-method abstract classes or interfaces, you'd use a delegate in C# instead, and often use a lambda expression to create instances. So something similar to... more 1/27/2015 1:44:36 PM

people

Warning as Error: Possible unintended reference comparison when upgrading from .Net 3.5 to .Net 4.5

Currently I am in the process of migrating a WPF application from .Net Framework 3.5 to .Net Framework 4.5. Next to the upgrade of the .Net Framework, the application will now be...
Jon Skeet
people
quotationmark

Your previous code was broken, basically. You say it worked, but I can't see that the condition would ever be false. I suspect the compiler is just smarter now than it was before. The condition... more 1/27/2015 1:26:42 PM

people

Trouble in invoking a method from one java class to another

I have a folder PackagesAndMethods . Within that folder I had two files TestMethods.java MyMethods.java The code within those files are, TestMethod.java package...
Jon Skeet
people
quotationmark

The problem is how you're compiling. You should generally be compiling from the "package root", ideally specifying an output root as well. For example, from the parent directory (D:\Java): > javac -d classes... more 1/27/2015 11:02:13 AM

people

how to run the main thread after all child threads have completed there exceution

I have a requirement in which 28 threads have to complete some functionality. I have created these threads as in anonymous inner classes like : Thread t=new Thread(new...
Jon Skeet
people
quotationmark

Note : I am confused about join() method as it makes my threads run sequentially. It will do that if you have code like this: for (Runnable runnable : runnables) { Thread t = new Thread(runnable); t.start(); ... more 1/27/2015 7:18:30 AM

people

How does Java know the types of objects and data when reading them back from a file

Hi I am new to Java and learning, I have searched for an answer to this question here and googled it, looked at the Java Doc but cant seem to find a clear explanation on exactly...
Jon Skeet
people
quotationmark

If there is mismatch in type or wrong sequence you get EOFException. No, that's simply not true - at least not in the general case. You'll get an EOFException if you reach the end of the data too early. If you just read the data with... more 1/27/2015 6:46:33 AM

people