Browsing 7239 questions and answers with Jon Skeet

Reading a file using fixed number of bytes java

I am writing a program to copy large files, so I want to read specific number of bytes and write to another file. I want to copy the file and get same number of bytes. But I am...
Jon Skeet
people
quotationmark

There are several problems with your code: You're using the default platform encoding to convert the binary data into text (by calling new String(byte[]) instead of specifying an encoding You're using the default platform encoding to... more 2/15/2015 10:07:41 AM

people

What is the size of a boolean In C#? Does it really take 4 bytes?

I have two structs with arrays of bytes and booleans: using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential, Pack = 4)] struct struct1 { ...
Jon Skeet
people
quotationmark

Firstly, this is only the size for interop. It doesn't represent the size in managed code of the array. That's 1 byte per bool - at least on my machine. You can test it for yourself with this code: using System; class Program { ... more 2/14/2015 10:01:41 AM

people

Use string as argument for GetValue()

I have a class Player, and an instance of that class called player. Inside that class, I have an auto-property called Name. I need to access the property Name from the instance....
Jon Skeet
people
quotationmark

Instances don't have names, generally. (You can create a class with a Name property, but that's nothing the framework is going to care about.) The value you have to pass to PropertyInfo.GetValue is a reference to the object whose property... more 2/14/2015 9:39:39 AM

people

Seeking better dictionary initialization

I recently answered a question here with a PowerShell dictionary that used "ContainsKey" to decide whether to define a key value or add to it if necessary. I do that a lot -...
Jon Skeet
people
quotationmark

ConcurrentDictionary in .NET will allow you to do that, yes: sums.AddOrUpdate(key, value, (k, v) => v + value); You should be able to use that (with syntax changes, obviously) in PowerShell, too. Alternatively, if you want to do... more 2/14/2015 8:06:42 AM

people

java.sql.Date (YYYY MM DD) to java.util.Date (DD MMM YYYY) conversion

Need advise how to send the java.sql.Date (YYYY-MM-DD) to java.util.Date (DD-MMM-YYYY). The class java.sql.Date is designed to carry only a date. I have a table with column...
Jon Skeet
people
quotationmark

I want to send the output in DD-MMM-YYYY format to the JSP page view. Then you need to use a SimpleDateFormat to do so. Currently you're using the result of Date.toString(), and ignoring temp which should already have the right... more 2/13/2015 5:48:30 PM

people

Java SimpleDateFormat unexpected parsing behaviour

I'm reading information from a file (.csv) that will be inserted to a database after validation and approval from the end user. The text file is read, validated and its...
Jon Skeet
people
quotationmark

No, it "becomes" a java.util.Date value. If you're then converting it back to a string by calling Date.toString(), you should consult the Date.toString() documentation for what to expect. The value stored in the Date is just a point in... more 2/13/2015 5:29:37 PM

people

Recommend best constructor with attribute?

Is there a way to denote the best constructor to use in C#, so that a developer knows which to pick by default? Say you have a class: public class MyClass { public...
Jon Skeet
people
quotationmark

Firstly, I'd advise you to chain your constructors the other way round - make the ones with fewer parameters chain (possibly indirectly) to a single constructor with all the parameters, and make that the only one which sets anything. That... more 2/13/2015 5:14:26 PM

people

Java Vs C# Long to DateTime Conversion

In Java I have the following test that passes fine // 42 bits of time is good enough for the next 100 years. // An IEEE double has 52 bits of mantissa, so our dates can be easily...
Jon Skeet
people
quotationmark

Your test is basically confusing ticks with milliseconds. If you only need to store a number of milliseconds since the unix epoch, then do so - but I'd recommend using something like this to perform the conversion: public static readonly... more 2/13/2015 4:40:25 PM

people

What is advisable to defer the checking an IllegalArgumentException?

Consider an example, of adding two hex numbers represented as char array: eg: 2A and 1D. Now, A upto F are valid, but G-Z are invalid for 16 bits. I have two cases below to throw...
Jon Skeet
people
quotationmark

In this case, there's no real harm done in terms of the behaviour due to checking later - it's not like the code which has executed so far in the method will have had an observable impact. You'll have wasted a small amount of time, but... more 2/13/2015 4:14:33 PM

people

LINQ statement needs to be optimized

I have the following code var x = from e in db.Employees where e.id == 746 join f in db.CoWorker on e.CoWorkerID equals f.ID into fa from fr in fa.DefaultIfEmpty() ...
Jon Skeet
people
quotationmark

Here's a query which performs the concatenation client-side instead. As you can see, it's more long-winded - you should absolutely check that it makes a significant difference before you decide to embrace it: var dbQuery = from e in... more 2/13/2015 2:25:43 PM

people