Browsing 7239 questions and answers with Jon Skeet

C# Tcp is losing packets?

I wrote a C# chat software that uses a new (at least for me) system that I called request system. I don't know if that has been created before, but for now I think of it as my...
Jon Skeet
people
quotationmark

You're assuming that you'll have one packet for each Send call. That's not stream-oriented - that's packet-oriented. You're sending multiple pieces of data which I suspect are coalesced into a single packet, and then you'll get them all in... more 2/24/2014 9:57:28 PM

people

How to set to int value null? Java Android

which is the best way to set already defined int to null? private int xy(){ int x = 5; x = null; //-this is ERROR return x; } so i choose this private int xy(){ ...
Jon Skeet
people
quotationmark

You can't. int is a primitive value type - there's no such concept as a null value for int. You can use null with Integer because that's a class instead of a primitive value. It's not really clear what your method is trying to achieve,... more 2/24/2014 9:49:22 PM

people

Difference between two times follow up (Convert to decimal)

I asked a question like this earlier and got a great answer but I made a mistake and wanted the output to be a decimal rather than a time. So here's the question. I have two...
Jon Skeet
people
quotationmark

It sounds like you want the total number of hours, in that 1.92 hours is 115 minutes (ish). In that case you want: double hours = (ts2 - ts1).TotalHours; ... you can then format that how you wish (e.g. to 2 decimal places). For... more 2/24/2014 9:14:53 PM

people

JSON string parse error. System.Exception {Newtonsoft.Json.JsonReaderException}

I have the following string. I am getting the following error. Could you please let me know what could be wrong? Unexpected character encountered while parsing value: C. Path '',...
Jon Skeet
people
quotationmark

You've included the HTTP headers in your JSON string - you don't want those. Your json value should just be this: {"name":"test44","age":"66","gender":"B","dob":"10\/10\/2003","file":null} I've tested your code, and when including the... more 2/24/2014 8:55:32 PM

people

Main Method parseBoolean not working

I need to parse my main method to see if the boolean is set to true or false, but every time i run it it is being set to false. when I go to run the program, this is my...
Jon Skeet
people
quotationmark

You're parsing "true," which is not the same as "true". Get rid of the comma in your invocation: java ScraperTestRunner1 true "/Desktop/imputfile.txt" Command line arguments aren't specified as a comma-separated list; they're... more 2/24/2014 7:48:47 PM

people

Is there any way how to test contracts set using Code contracts?

I would like to write unit test that verify that my method does not accept invalid arguments. Validity of arguments is checked using Code Contract's Contract.Requires call. Why...
Jon Skeet
people
quotationmark

You can't explicitly catch the right exception type, but you could catch Exception and then check that it's a ContractException using reflection (rethrowing otherwise). That would be ugly to do everywhere, but you just need to do it... more 2/24/2014 7:18:44 PM

people

Differences in array declaration

Recently I came across the types of array declaration in java like, int[] arr = { 1, 2, 3, 4, 5 }; int[] arr1 = new int[5]; arr1[0] = 0; arr1[1] = 1;...
Jon Skeet
people
quotationmark

They're equivalent (assuming you actually change the values in the first one to 0, 1, 2, 3, 4.) Indeed, they'll even compile to nearly the same bytecode; Java bytecode doesn't have anything cunning to make this simpler, so the compiler... more 2/24/2014 6:34:20 PM

people

C# issue with dynamically linking variable value in delegate

I have a GUI setup code in my game, like: for (int i = 0; i < count; i++) { ... Button button = new Button(); ... button.handler = delegate { selectedIndex =...
Jon Skeet
people
quotationmark

You're running into the common problem of what gets captured in an anonymous function. You're capturing i in the delegate, and that value changes over the course of your loop. You need a copy of i: for (int i = 0; i < count; i++) { ... more 2/24/2014 11:44:15 AM

people

Correct way to boxing bool[] into object[] in C#

I want to find the best approach for converting bool[] into object[] in C# .NET 4.0. Now I have this variables: object[] objectArray = new object [] { true, false, true...
Jon Skeet
people
quotationmark

It sounds like you just need to box each value, right? That's as simple as: object[] objectArray = boolArray.Select(b => (object) b).ToArray(); Or even: object[] objectArray = boolArray.Cast<object>().ToArray(); (As Cast... more 2/24/2014 11:22:11 AM

people

Casting error with uploading files

I was working on my website when i noticed my foreach loop wasn't working to upload a list of files and a for loop did work. I am curious to find out why the for loop works and...
Jon Skeet
people
quotationmark

As soon as you look at HttpFileCollection's documentation, it becomes clearer... The indexer has a declared type of HttpPostedFile. Whereas GetEnumerator states: This enumerator returns the keys of the collection as strings. So... more 2/24/2014 10:11:43 AM

people