Browsing 7239 questions and answers with Jon Skeet

Java: Getting a variable name from a string

I have some variables defined like this. public final static String pDrive = "\\\\fs-innova1\\projects\\"; public final static String hDrive =...
Jon Skeet
people
quotationmark

You should use a Map<String, String> instead: private static final Map<String, String> DRIVE_MAPPINGS = new HashMap<>(); static { DRIVE_MAPPINGS.put("p", "\\\\fs-innova1\\projects\\"); DRIVE_MAPPINGS.put("h",... more 10/30/2013 5:33:33 PM

people

Unable to convert bytes from charset 65535 in to Japanese (5035)

I have a character set conversion issue: I am updating Japanese Kanji characters in DB2 in iSeries system with the following conversion method: AS400 sys = new...
Jon Skeet
people
quotationmark

Well I don't know anything about CharConverter or AS400Text, but code like this is almost always a mistake: String s = new String(bytes); That uses the platform default encoding to convert the binary data to text. Usually storage and... more 10/30/2013 5:16:03 PM

people

Java custom comparator with different sort options

I have a an Item class with two properties - id and timestamp. There is a custom comparator class to sort an itemList according to the timestamp. Is there a way to use the...
Jon Skeet
people
quotationmark

Can I use the same comparator to sort the list by Id too? Well you could have a constructor parameter to say which property you want to sort by - but personally I'd create a different class for that instead. You can have as many... more 10/30/2013 4:18:46 PM

people

Should a string constants class be static?

I am working on a new project and I have noticed some code that I am not sure is true. The names and values I am using to demonstrate the question are fake. public class MyConsts...
Jon Skeet
people
quotationmark

Yes, it makes sense for it to be static. That signifies your intention, prevents clients from even declaring a variable of that type, etc. You'll need to move the const modifier before the type part though: public const string MyConst1 =... more 10/30/2013 3:54:17 PM

people

What are the implications of using lock(typeof(string))

We had a discussion at work regarding locking and what exactly happens. The code that triggered this discussion is: string name = (string)context.Cache[key]; if...
Jon Skeet
people
quotationmark

My question is what exactly does lock(typeof(string)) do? It locks on the Type object referred to by the reference that the typeof operator returns. That means any code which does the same thing anywhere within the same process (or... more 10/30/2013 3:45:48 PM

people

How do I read/input data in .NET 3.5?

I'm used to .NET 4.0, but came across a situation where I have to revert to .NET 3.5. In my 4.0 code I would rely on this to read my data: var Data =...
Jon Skeet
people
quotationmark

Well you can use File.ReadAllLines instead - that will read the whole file into a string[] rather than reading it line by line, but you're reading the whole thing into memory anyway due to the ToArray call, so it's just a bit less... more 10/30/2013 3:25:11 PM

people

subclassing a class with a private constructor

implicit super constructor Example() is not visible for default constructor.Must define an explicit constructor. Is it so because I am defining a private constructor and then...
Jon Skeet
people
quotationmark

Is it so because I am defining a private constructor and then trying inherit the class in other class? Yes. If your only constructors are private, then you can't create a subclass of that class, as there would be no accessible... more 10/30/2013 11:13:40 AM

people

Calculate months between two dates keeping year in mind using JodaTime

I am using this code to calculate no of months between input pastdate and currentdate. It uses JodaTime LocalDate date1 = new LocalDate(installmentStartDate2); LocalDate...
Jon Skeet
people
quotationmark

You're asking for the difference in years, months and days - so you're getting back 1 year, 10 months and 29 days. Just use: int months = Months.monthsBetween(date1, date2).getMonths(); Or if you really want to use new Period and... more 10/30/2013 9:00:38 AM

people

How to create an async method that can return the non void value?

I try to write a class with method that uses some awaitable operations. This condition makes me define this method as async. But I can't return non-void value from this method....
Jon Skeet
people
quotationmark

But I can't return non-void value from this method Yes you can, but it has to be Task or Task<T> for some T. So you can change your method to: public async Task<BitmapImage> GetThumbImageFromFile(StorageFile... more 10/30/2013 8:23:29 AM

people

Unexpected difference in dates parsed using yyyy MM dd hh:mm:ss format

I've run the below java code to get time difference. import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; public...
Jon Skeet
people
quotationmark

This is the problem: new SimpleDateFormat("yyyy-MM-dd hh:mm:ss") The hh here means "12 hour hour-of-day" so 12 means midnight unless there's something to indicate that it's meant to be 12 PM. Your value of 13 only works because the... more 10/30/2013 7:14:51 AM

people