Browsing 7239 questions and answers with Jon Skeet

How to convert an arbitrary object to enum?

I have an object. Usually it is either long or string, so to simplify the code let's assume just that. I have to create a method that tries to convert this object to a provided...
Jon Skeet
people
quotationmark

It feels to me like you only actually want to handle three cases: Input is already the right type Strings Integers in various types I believe this will do what you want for valid input: public object ToEnum(Type enumType, object... more 1/31/2014 2:32:23 PM

people

Visual studio 2010: Nunit test case fails in debug mode but passes in run mode

I'm having a weird problem with Visual Studio 2010 Ultimate: One of my Nunit(2.6.2) test cases is failing in the debug mode but passing in the run mode, as if we had completely...
Jon Skeet
people
quotationmark

You may have run into a situation where you're taking the same code path, but the results are subtly different in debug vs non-debug due to optimizations. There are a couple of distinct possibilities here: Your code has a subtle bug,... more 1/31/2014 12:45:46 PM

people

Values of process.exitValue() in JAVA

This question is about the values of process.exitValue(). If process.exitValue()=0 its ok, if it's -1 something is wrong, but if it's something else, what does it mean? For...
Jon Skeet
people
quotationmark

That's up to the process in question. Even the "0 means success" is a convention more than anything else - although it's a very common one. In general I would assume that any non-zero value is an error of some description; look at the... more 1/31/2014 12:23:01 PM

people

remove specific object does not work in Java ArrayList

I have an ArrayList of object containing a reference to their parent object. I am trying to remove every objects within a specific parent (so equals to the parent object). If I...
Jon Skeet
people
quotationmark

I suspect the problem is that after you've removed element i (which moves everything after it up the list - the element at index n + 1 now has index n etc) you're then skipping the next element. So if you have two consecutive elements to... more 1/30/2014 9:33:04 PM

people

Output to the screen without moving to a new line

Console.WriteLine automatically move text on a new line and I have output on the console like: 1 2 3 4 5 .. But I need: 1234 5.. Code: for (int i = 0; i < 5; i++) { ...
Jon Skeet
people
quotationmark

If you don't want to write a line, don't use Console.Write*Line*. Just use Console.Write: for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { Console.Write(aField[i, j]); } ... more 1/30/2014 9:28:23 PM

people

Wait for an Async method to finish from a separate method

Using PostSharp to wrap a WCF Service call with AOP Logging with the OnEntry and OnExit methods for OnMethodBoundaryAspect. Process: OnEntry() is called, a DateTime...
Jon Skeet
people
quotationmark

EDIT: Okay, I think I misunderstood you. Basically, you need to remember the Task that you've started, and then you can just call Wait() to wait until it's finished: class LogMethodCallAttribute : OnMethodBoundaryAspect { Task task; ... more 1/30/2014 8:51:23 PM

people

C# Checking if a query returns empty set not working?

I have been working on trying to check if a record exists in a database table. I have the following code and it is not working. MySqlConnection connection = new...
Jon Skeet
people
quotationmark

There are at least two issues here. Firstly, you're calling BeginExecuteNonQuery - that doesn't return an integer, it returns an IAsyncResult. I strongly suspect that you don't want to be dealing with the asynchronous API at... more 1/30/2014 6:49:15 PM

people

C# Does this object initiation call its constructor?

private Object object1; Does object1 call the default constructor of Object even though it haven't used = new Object(); ? If not, is object1 NULL until it is initialized with...
Jon Skeet
people
quotationmark

No, that field declaration just declares a field. It will have a default value of null. From section 5.2 of the C# 5 specification: The following categories of variables are automatically initialized to their default values: ... more 1/30/2014 5:30:08 PM

people

Recursively calling an async void: any guarantee on the stack limit?

Just facing this curious case, and I wonder whether this pattern will yield any guarantee on the stack usage (i.e. is reliable or not). NOTE: the counter is just an hypothetical...
Jon Skeet
people
quotationmark

No, you'll end up with a lot of continuations scheduled on a lot of tasks, rather than a deep stack... at least in the initial call. Note that this is only because you're using await Task.Delay(1000); ... which can reasonably be... more 1/30/2014 4:03:07 PM

people

SQL Variable current date 1 day

I have a variable that retrieves the current date. select dateformat(getdate(), 'yyyy-mm-dd hh:nn:ss.SSSSSS') What i want to do is change this so it inserts the current date...
Jon Skeet
people
quotationmark

I suspect you want DATEADD: select dateformat(dateadd(dd, -1, getdate()), 'yyyy-mm-dd hh:nn:ss.SSSSSS') more 1/30/2014 2:49:34 PM

people