Browsing 7239 questions and answers with Jon Skeet

Where to put Using block when open Process?

I have process that i am opening and create file on dist and subscribe to this procees ProcessExitedEvent: public int InvokeProcess(WiresharkProcesses process, string args) { ...
Jon Skeet
people
quotationmark

I very much doubt that a using statement will help here. Process.Dispose doesn't kill the other process - it just releases the CLR handle to the other process. (To be honest, I don't remember every seeing anyone use a using statement for... more 10/15/2013 3:39:40 PM

people

Callbacks in C# taking the callback directly in the method like in javascript

I have the function in javascript which is something like dothis(variablea, function(somevalue) { .. }); which comes from function dothis(variablea, callback) {..} So I...
Jon Skeet
people
quotationmark

Absolutely - you basically want delegates. For example: public void DoSomething(string input, Action<int> callback) { // Do something with input int result = ...; callback(result); } Then call it with something like... more 10/15/2013 3:06:23 PM

people

Difficulty with Conversion of Date Time to JSON data

I am trying to convert a list<T> into JSON data and then binding to Jqgrid. The Problem is that list collection contains one of the column as DateTime. When i see the...
Jon Skeet
people
quotationmark

When you say it's "not properly serialized" - that looks okay to me. That's one format for JSON dates, coming from the Javascript Date constructor taking a "milliseconds since the Unix epoch" as a parameter. So the value you've given is... more 10/15/2013 9:27:52 AM

people

Removing elements from ArrayList

I'm trying to remove certain elements from an ArrayList<String> for(int i=0; i<myList.size(); i++) { if(myList.get(i).contains("foo")) { ...
Jon Skeet
people
quotationmark

This however leaves "empty spaces" in my list. No, it doesn't. It removes entries from the list completely. Other elements are moved appropriately. What it does do with the way you've written is skip the check for the next entry...... more 10/15/2013 8:54:34 AM

people

Binary Search and Hashtable Search

I wanted to find out the trade off point between a Dictionary lookup and a binary search lookup of an array. I was expecting constant time lookups for the Dictionary, and...
Jon Skeet
people
quotationmark

(Pretty much as noted in comments.) I suspect you're mostly seeing the effects of cache misses. When the collection is large, you'll get a lot of cache misses - particularly with the binary search, which potentially needs to touch a lot... more 10/15/2013 6:19:05 AM

people

How to Convert image file to binary format

I want to convert any image file from my system to binary format by giving the path of the image from the system. After converting I want to store the converted binary format in a...
Jon Skeet
people
quotationmark

If you're really just trying to store a file in a database, you don't need to worry about it being an image at all. The image file will already be in a "binary format" - you just need to store the file data. There may well be ways of doing... more 10/15/2013 6:11:22 AM

people

C#: alternative to exists or getfirstordefault

I have a class class Person { int Age; string Name; } List<Person> pList = new List<Person>(); pList.Exists(p=> p.Name == "testName"); <-- need an...
Jon Skeet
people
quotationmark

Exists should be fine - you just need to handle the possibility of p being null. bool nameExists = pList.Exists(p => p != null && p.Name == "testName"); Alternatively, make sure that your list doesn't contain any null... more 10/14/2013 9:30:41 PM

people

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Every time I hit the calculate button I receive the following message: An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information:...
Jon Skeet
people
quotationmark

You're converting a number to an integer. So if you entered (say) 9.5 that would fail, because it's not an integer. You should almost certainly be using decimal.TryParse, noting that the return value will say whether parsing succeeded or... more 10/14/2013 6:59:20 PM

people

Adding objects to array list end up with same values

Bit of an odd one. I am trying to create a board game (backgammon) and have created an object called piece. On my board I am trying to create an array list of...
Jon Skeet
people
quotationmark

The ArrayList is a red-herring here - you're never even fetching the pieces out again. You'll see exactly the same behaviour with just this code: piece newPiece = new piece(1, 1, "red", "top"); piece newPiece2 = new piece(1, 2, "black",... more 10/14/2013 4:06:17 PM

people

Basic Java threading and runnable pattern

I have a problem with understanding this code. I have only a couple of hours' knowledge of Java. Here is the code : // Create a new thread. class NewThread implements...
Jon Skeet
people
quotationmark

t.start() creates a new thread, and calls run() from that. At that point, there are two threads running independently: the one that called start(), and the new one. The original thread returns from the constructor and then starts executing... more 10/14/2013 3:40:17 PM

people