Browsing 7239 questions and answers with Jon Skeet

declaring a MySqlDataReader without initialising it

I want to declare a MySqlDataReader, without initialising it or assigning any value to it. Like the code below. MySqlDataReader rdr; try { /* stuff to open the MySqlDataReader...
Jon Skeet
people
quotationmark

One option is to initialize it to null to start with: MySqlDataReader rdr = null; After all, you're already checking whether it's null within the finally block in your sample code, so that's fine. It's not clear why you're not just... more 6/23/2014 10:34:54 AM

people

assign a variable to JodaTime

Consider I have a value which i have to set as day in JodaTime. int new_day; this is my day which I have to set as day in JodaTime. I already have a time in JodaTime object...
Jon Skeet
people
quotationmark

If your start variable is of time DateTime, I suspect you want DateTime.withDayOfMonth: start = start.withDayOfMonth(new_day); Other types (e.g. LocalDateTime) have similar methods. (I'd also advise you to start following Java naming... more 6/23/2014 9:20:22 AM

people

CultureInfo.InvariantCulture in .ToString()

I am currently fixing FxCop issues so I encountered issue where I have to provide cultureinfo when converting a string using ToString() . Currently in my code nothing we are...
Jon Skeet
people
quotationmark

Is default and CultureInfo.InvariantCulture are one and the same? No, absolutely not. The default culture depends (initially) on the operating system settings. The invariant culture is meant to be a "neutral" culture. Your example of... more 6/23/2014 8:54:28 AM

people

Write Delegates without messing up the code

I am writing delegates as like this. delegate void MyMethod(string arg1, string arg2); MyMethod mm; I don't know why it needs two lines to declare a single delegate. If my...
Jon Skeet
people
quotationmark

You're declaring two very different things here: The first line declares a delegate type called MyMethod The second line declares a field of that delegate type It's important to understand the difference, because then you can work out... more 6/23/2014 6:13:04 AM

people

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery

I want to get record from my db where hidden=false and id is got in browser. Please see my code and fix for me, thanks so much (I searched but can not fix my error). My...
Jon Skeet
people
quotationmark

Currently you're trying to pass in a model which is a query, whereas your view is expecting a single model object. Currently your check for nullity is pointless, too - the result of Where is never null... it's just a query. Instead, you... more 6/22/2014 2:33:53 PM

people

Separate chaining for HashTables in Java

Based on the following code snippet : Hashtable balance = new Hashtable(); Enumeration names; String str; double bal; balance.put("Zara", new Double(3434.34));...
Jon Skeet
people
quotationmark

Does Java use separate chaining only for collision handling? Yes. You can only have one entry per key in a Hashtable (or HashMap, which is what you should probably be using - along with generics). It's a key/value map, not a... more 6/22/2014 8:15:36 AM

people

Getting array index out of bounds exception on this

String resultArr[] = response.split("&"); String[] values = resultArr; String name[] = new String[values.length/2]; for(int i = 0;i<values.length-1;i++) { if(i%2==0) ...
Jon Skeet
people
quotationmark

This is the problem: name[i] = values[i]; Your name array is only half the size of your values array, but you're trying to use the same indexes. I suspect you want: name[i / 2] = values[i]; Or more readably in my view: // Note... more 6/22/2014 7:53:48 AM

people

Working with methods that return anonymous methods

If I have a class like this: public class SomeClass { public Action<string> SomeAction { get; set; } public SomeClass() { SomeAction =...
Jon Skeet
people
quotationmark

Well, that's nearly all that happens. In this particular case, your lambda expression (it's not an anonymous method, to be picky) doesn't need any state, so the generated method can be static, and a delegate reference can be cached. So the... more 6/22/2014 7:50:35 AM

people

FileStream not working properly

I am trying to read/write files using ReadBytemethod. code is working but I have noticed that they are not available after process.I cant open them.I images not displaying.what am...
Jon Skeet
people
quotationmark

You're only reading a single byte - I suspect you meant to write a while loop instead of an if statement: while (fsRead.Position != fsRead.Length) { byte b = (byte)fsRead.ReadByte(); fswrite.WriteByte(b); } However, that's still... more 6/22/2014 7:43:39 AM

people

Why is catching a RuntimeException not considered a good programming practice?

Why is catching a RuntimeException using catch(Throwable exc) {} not considered a good programming practice? What is the right way to handle RuntimeExceptions? Also, why does...
Jon Skeet
people
quotationmark

Usually, a RuntimeException indicates a programming error (in which case you can't "handle" it, because if you knew to expect it you'd have avoided the error). Catching any of these general exceptions (including Throwable) is a bad idea... more 6/21/2014 6:24:28 PM

people