Browsing 7239 questions and answers with Jon Skeet

How to get calender date from a date field in Java in a particular time zone?

I have a date stored in a date variable(java.util.Date), (say 2015-4-4 15:30:26-0700) in yyyy-mm-dd hh:mm:ss timezone format. Now if I try to get the calendar day (i.e. 04 april...
Jon Skeet
people
quotationmark

I have a date stored in a date variable(java.util.Date), (say 2015-4-4 15:30:26-0700) in yyyy-mm-dd hh:mm:ss timezone format. No, you have a Date variable. That doesn't have any particular format. Ignore what toString() tells you -... more 7/28/2015 12:47:04 PM

people

Is implementation of a standard constructor (Java) necessary in this case?

I have three classes like this: public abstract class ClassA extends ClassX { protected ClassA() { super(); } // more code } public class ClassB extends...
Jon Skeet
people
quotationmark

Now I'd say that I can't do the same for ClassB since the accessibility of the constructor is increased from protected to public. That's irrelevant, given that it's an abstract class. The constructor can't be called directly - it's... more 7/28/2015 12:32:25 PM

people

if Statement within List View Controller Method

I have the following code within my controller method to provide data to the List View of membership Types: [HttpGet] public ActionResult SelectMembershipType(int clubId) { ...
Jon Skeet
people
quotationmark

The if statement Ive put in doesn't work it throws several errors. Yes, it would - you've put it in an object initializer. Object initializers aren't arbitrary blocks of code - they're a list of property = value assignments,... more 7/28/2015 12:23:50 PM

people

Unboxing for dynamic type

Consider the following code: public class Foo1 { public dynamic dowork() { return 10; } } And in my Main , I call like: int i = new...
Jon Skeet
people
quotationmark

It is unboxing - but it's doing it implicitly. There's an implicit conversion from any dynamic expression to any type. The exact conversion performed will depend on the execution-time type of the value. From section 6.1.8 of the C# 5... more 7/28/2015 12:16:27 PM

people

Is it OK to change values of optional parameters

Changing parameter values is considered an anti-pattern, but I find it useful sometimes with optional parameters in C#: public void Foo(int p1, MyClass fooObj = null) { if...
Jon Skeet
people
quotationmark

That's absolutely fine. In fact, it's a good way of making a parameter optional without having to bake in the value as a constant. You can use the null-coalescing operator to make it slightly more readable though: fooObj = fooObj ??... more 7/28/2015 12:05:51 PM

people

FileNotFound while using InputSource

I get an Error when using InputSource for XPath. InputSource inputSource = null; try { inputSource = new InputSource(new FileInputStream(filename)); } catch...
Jon Skeet
people
quotationmark

In readFromFile you're calling openFileInput, instead of the FileInputStream constructor. So do the same thing when you want to create an InputSource: inputSource = new InputSource(openFileInput(filename)); more 7/28/2015 9:50:40 AM

people

java.io.IOException: Stream closed at java.util.zip.ZipInputStream.ensureOpen(ZipInputStream.java:66)

Below is the code Snippet. while (iterator.hasNext()) { FileItemStream item = iterator.next(); InputStream in = item.openStream(); ZipInputStream zin =new...
Jon Skeet
people
quotationmark

You're calling the POIFSFileSystem(InputStream) constructor, which is documented as: Create a POIFSFileSystem from an InputStream. Normally the stream is read until EOF. The stream is always closed. (Emphasis mine.) That means... more 7/28/2015 5:59:47 AM

people

duplicate key value violates unique constraint (primary key)

Hi I have a program here that stores the checked values in checkedlistbox to database. The problem is I always encounter an exception saying "duplicate key value violates unique...
Jon Skeet
people
quotationmark

Look at this loop: for (int i = 0; i <= checkedListBox1.CheckedItems.Count - 1; i++) { NpgsqlCommand cmd = new NpgsqlCommand("Insert into famhistory(famcon) Values (@famcon)", conn); cmd.Parameters.AddWithValue("@famcon",... more 7/28/2015 5:48:18 AM

people

c# parameter implicit conversion

Having this code: class Program { static void Main(string[] args) { Check(3); Console.ReadLine(); } static void...
Jon Skeet
people
quotationmark

Why this code prints "I am an int" and not "I am a long" ? Because the compiler goes through the rules of overload resolution, which are in the C# 5 spec, starting at section 7.5.3. Both of those are applicable function members (i.e.... more 7/27/2015 8:12:37 PM

people

.ToString() does not raise an exception on double? or long? while it will raise an exception on null string

I have three properties as follow:- public Nullable<double> SPEED { get; set; } public Nullable<Int64> PROCESSORCOUNT { get; set; } public string CPUNAME { get; set;...
Jon Skeet
people
quotationmark

To simplify it: int? x = null; string result = x.ToString(); // No exception Here null isn't a null reference. It's just a null value of the type int?, i.e. a value of type Nullable<int> where HasValue is false. You're just... more 7/27/2015 2:30:07 PM

people