Browsing 7239 questions and answers with Jon Skeet

Does "using" block release resource when program force closed

Question as per title. I have a piece of code that does this: using (SqlConnection dbcon = new SqlConnection(connectionString)) using (SqlDataAdapter dataAdapter = new...
Jon Skeet
people
quotationmark

If a process exits, all the native resources (network handles, file handles etc) are cleared up by the operating system. I wouldn't expect the using statement to come into effect here - I suspect the OS will kill the threads too hard for... more 9/29/2015 5:53:11 AM

people

Xelement.Element(string) always returning null

I have an Xelement which looks like: <headers batchid="123456" xmlns="http://api.temp.com/ns/"> <header uri="https://api.temp.com/v1.0/headers/147852" id="147852"...
Jon Skeet
people
quotationmark

You're ignoring the namespace of the element. Due to the xmlns="...", both the headers and header elements are in a namespace of "http://api.temp.com/ns/". Fortunately, LINQ to XML makes this really easy to fix: XNamespace ns =... more 9/28/2015 3:50:42 PM

people

Java 8 LocalDateTime is parsing invalid date

I wanted to validate date in client side so I wrote the following code. But instead of getting an exception I am getting a proper date object for 31st of February date string,...
Jon Skeet
people
quotationmark

You just need a strict ResolverStyle. Parsing a text string occurs in two phases. Phase 1 is a basic text parse according to the fields added to the builder. Phase 2 resolves the parsed field-value pairs into date and/or time objects.... more 9/28/2015 12:53:41 PM

people

Java nested scopes and variables' name hiding

I'm learning name look up in Java, and coming from C++ I found interesting that even if Java lets me nest however many blocks of code,I am allowed to hide a name only in the first...
Jon Skeet
people
quotationmark

It's not about the "first nested scope" - it's a matter of Java allowing a local variable to hide a field, but not allowing it to hide another local variable. Presumably the designers of Java believed such hiding to be bad for... more 9/28/2015 9:25:12 AM

people

Java assigning values to variables in an Interface class through xml

Is it possible to change the value of variables in an interface class using XmlDecoder and XmlEncoder?. I have an interface class that contains variables that needs to be...
Jon Skeet
people
quotationmark

I have an interface class the contains variables that needs to be implemented by other classes. Interfaces can't contain variables as such - they can only contain constants, so it makes no sense to try to change the value of... more 9/28/2015 8:19:47 AM

people

C# List comparison between custom objects

Pair<BoardLocation, BoardLocation> loc = new Pair<BoardLocation, BoardLocation>( this.getLocation(), l ); if(!this.getPlayer().getMoves().Contains( loc )) { ...
Jon Skeet
people
quotationmark

You can definitely make this code a lot simpler by using && and just returning the value of equality comparisons, instead of all those if statements and return true; or return false; statements. public override bool Equals (object... more 9/26/2015 3:46:41 PM

people

Iterator seems to be losing data java

I've got a method that returns a list of hashmaps (the data comes from a ResultSet). While trying to use an iterator to loop through the data and convert it to a String array, I...
Jon Skeet
people
quotationmark

You're calling edgesIterator.next() three times for each iteration of the loop in your first snippet, which I'm pretty sure you don't want to do. Just don't do that: while (edgesIterator.hasNext()) { HashMap tmp =... more 9/26/2015 5:21:19 AM

people

jUnit gets NullPointerException when using File functions

If you need any other information please let me know. Short Version: This line of code causes a NullPointerException when tested with jUnit 4.12: File[] files = new...
Jon Skeet
people
quotationmark

The documentation for File.listFiles() includes: Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs. So it looks like that's what's happening. A path of d:\..\core\files doesn't look like a... more 9/25/2015 1:45:21 PM

people

Cast object to KeyValuePair with "generic value"?

I have a ComboBox filled with mixed items of two different types. The types are either KeyValuePair<Int32, FontFamily> or KeyValuePair<Int32, String> Now there...
Jon Skeet
people
quotationmark

You certainly don't need to use GetType(). You could use: int key; var item = combobox.SelectedItem; if (item is KeyValuePair<int, FontFamily>) { key = ((KeyValuePair<int, FontFamily>) item).Key; } else if (item is... more 9/25/2015 12:11:08 PM

people

Java UTF 16 conversion to UTF 8

Step 1: Making a REST call using HttpClient to Twitter endpoint and getting a tweet message containing an emoticon. Twitter APIs returns the string with UTF-8 encoding. Example:...
Jon Skeet
people
quotationmark

You've got a string with length 2 - because the length() property returns the number of UTF-16 code units, not the number of Unicode characters. Bear in mind that a String in Java is really a sequence of UTF-16 code units, not a sequence... more 9/25/2015 11:47:57 AM

people