Browsing 7239 questions and answers with Jon Skeet

Is it possible to have a constructor on a generic class that introduces additional generic types?

I'm trying to build a generic class whose constructor introduces an additional type, but the compiler says no-no. I don't quite understand why the following doesn't work: public...
Jon Skeet
people
quotationmark

No. Only types and methods can be generic. You can't have generic properties, events, fields, constructors or finalizers. Note that this has nothing to do with whether the containing type is generic or not - it's just a limitation of the... more 11/16/2013 9:20:24 PM

people

Date parsing error while formating a custom Date

I want to convert this date "Sat, 16 Nov 2013 20:09:07" to unixtime my code SimpleDateFormat pattern = new SimpleDateFormat("EEE, dd MM yyyy HH:mm:ss"); Date date =...
Jon Skeet
people
quotationmark

Yes - you're using MM which is a numeric month pattern. You want MMM instead, as that's "abbreviated month name". I'd also suggest explicitly specifying Locale.US in the SimpleDateFormat constructor if you know that the month and day names... more 11/16/2013 8:51:08 PM

people

Java: HashSet duplicating last value

for some reason all entries I am putting into a HashSet are duplicating the last entry (Even though they're different). I don't know why this is and have tried many things to fix...
Jon Skeet
people
quotationmark

Given this code: Arena.name = name; ... it looks like your name variable is static. (The same is true for some of your other variables too.) That means that rather than each Arena instance having a separate name, you have a single... more 11/16/2013 5:42:56 PM

people

XML Name Space Not Formating Correctly

Building an XML XDocument to push to web service and the root element needs namespace values this is what the XML form should look like.... <shipment-feed...
Jon Skeet
people
quotationmark

This sort of the thing is the problem: new XElement("shipment", ...) You want the shipment elements to be in the "http://seller.marketplace.sears.com/oms/v5" namespace - so you need to make that explicit. That won't show up in the... more 11/16/2013 5:02:01 PM

people

Is there is an oposite tool to static initializer in Java

Static blocks are executed when class is loading even before the public static void main starts. Is there any possibility to run a certain code just before the program close?
Jon Skeet
people
quotationmark

I think you're looking for Runtime.addShutdownHook. It's not a language level construct in the way that static initializer blocks are, but I think it'll do what you want. You should be careful with shutdown hooks though - see the... more 11/16/2013 4:12:13 PM

people

C# Input system Since '{method}' returns void, a return keyword must not be followed by an object expression

Trying a shot to a little advanced text adventure here, I have an inventory class. (isn't an error) and it all works great! I'm trying to implement a feature of an input. That it...
Jon Skeet
people
quotationmark

Turned out a 'void' method can't return something. I don't know what I should use then. You should use a method which is declared to return the kind of information you want to return! When a method is void, that specifically means... more 11/16/2013 3:09:44 PM

people

I can link a .jar on compile time but not on run time

I compile java from the command line. I can run: javac test.java -cp jarfile.jar Now i thought i could run (from the same folder) java test -cp jarfile.jar I also tried...
Jon Skeet
people
quotationmark

The first problem is that you're specifying the class name before the classpath - so it thinks that -cp is one of the command line arguments to your class, rather than an argument to the JVM to say where to find classes. The second... more 11/16/2013 11:46:42 AM

people

Why we do SQLiteCommand, Parameters.add while we can use string.Format to compose sql statement?

I saw in many tutorial that compose sql statement by using variable and Parameters.Add likt this public void updateStudent(String @studentID, String @firstName, String...
Jon Skeet
people
quotationmark

Four reasons: Avoiding SQL injection attacks Avoiding problems with strings containing genuine apostrophes with no intention of causing a SQL injection attack (e.g. a last name of "O'Reilly" Avoiding string unnecessary conversions, which... more 11/16/2013 11:11:05 AM

people

Generic method with sqlite net

I want to make a simple repository and underneath there is the method I have used: public static List<T> GetAll() { return...
Jon Skeet
people
quotationmark

It sounds like you just need to add another constraint to T - the Table<T>() method appears to want the new() constraint, so you'll need to add that to your constraint list too: class FeedRepository<T> :... more 11/16/2013 10:31:56 AM

people

A way to notify parent thread when child thread was killed or interrupted

I have a multithreading application in C#. Child threads do some serious work and I want know when child thread was killed to rerun it. What is the correct way to do it?
Jon Skeet
people
quotationmark

You'd be best off using the Task Parallel Library introduced in .NET 4. That way you can add a continuation to execute on failure (or cancellation, or success). Call Task.ContinueWith and pass in the continuation. Tasks are generally the... more 11/16/2013 10:17:03 AM

people