Browsing 7239 questions and answers with Jon Skeet

HashSet calls overridable method in constructor

It is bad practice to call overridable methods in a constructor (see this link). This will always call the method defined in the class and not a derived class' method. In Java's...
Jon Skeet
people
quotationmark

The answer is that it does break subclasses, potentially. It's easy to show that: BrokenSet.java: import java.util.*; public class BrokenSet<E> extends HashSet<E> { private final List<E> list = new... more 5/2/2015 7:06:23 PM

people

Is it possible to have different value of static variable for every instance?

I have a class with some static variables and I want to create multiple independent instances of this class. Independent meaning, that they do not share values of these static...
Jon Skeet
people
quotationmark

I have a class with some static variables and I want to create multiple independent instances of this class. In that case you just don't want static variables. static means "associated with the type rather than an instance of the... more 5/2/2015 3:36:14 PM

people

Parallel Webservice loop skipping

I'm using a parallel loop to call a webservice because the individual for loop is too slow. However the results comes out skipping some of the item. Code: private void...
Jon Skeet
people
quotationmark

It's not at all clear that you should expect Literal1.Text += ... to be thread-safe. I would suggest you use the Parallel.For loop just to collect the data, and then change Literal1.Text afterwards. For example, you could write: var... more 5/2/2015 9:17:31 AM

people

How to assign one value to multiple variables

I want to receive one value, which represents multiple variables. for example I receive 110200john This value goes directly without any code to multiple variables like int x =...
Jon Skeet
people
quotationmark

You should almost certainly create a class to represent those three values together, if they're meaningful. I'd personally then write a static parse method. So something like: public final class Person { private final int x; ... more 5/2/2015 8:46:28 AM

people

I want to Create Car object using Vehicle class ( i.e Vehicle c = new Car() ), and insert it into the list correctly

I created a Car object using Vehicle class ( i.e Vehicle c = new Car() ), but my problem is how I will insert it into the list then Prompt the user to enter details for this...
Jon Skeet
people
quotationmark

You've created an ArrayList<Car> - you can only add elements to that list that the compiler knows to be cars. So you could use: list.add((Car) c); or more simply, just declare the type of c to be Car to start with: Car c = new... more 5/2/2015 7:28:08 AM

people

Java SimpleDateFormat parsing oddity

I am baffled why this doesn't work. No matter what I change the day to, it prints out the same thing. What is going on here? SimpleDateFormat sdf = new...
Jon Skeet
people
quotationmark

You're using YYYY which is the week-year instead of the "normal" year. That's usually only used with day-of-week and week-of-year specifiers. The exact behaviour here would be hard to explain - feasible, but not terribly helpful.... more 5/1/2015 2:44:53 PM

people

Why are we unable to add non generic extension methods to generic objects?

I finally got tired of IEnumerable not having an Add method, and decided to add my own through an extension method. My initial attempt was something along these lines: public...
Jon Skeet
people
quotationmark

Well, you need to ask yourself what's special about T here. Suppose we were to write: public static void Add(this IEnumerable<X> items, X item) ... would you expect that to work? If so, consider: public static void Add(this... more 5/1/2015 12:48:20 PM

people

Prevent ObsoleteAttribute warning bubbling up call stack

I have a class using a deprecated assembly (System.Data.OracleClient). At compile time this results in a message 'MethodName()' is obsolete: 'OracleConnection has been...
Jon Skeet
people
quotationmark

Can I get round this so that I only suppress the warning in the method where the assembly is called? Yes - by using the normal way of suppressing warnings in C# - using #pragma warning disable. Here's an example: using... more 5/1/2015 10:33:55 AM

people

Custom Type ArrayList with IEnumerator

See .net fiddle link https://dotnetfiddle.net/E4eCWl I have a homework assignment to use an ArrayList of type Book so while I know List<T> would be the correct way to...
Jon Skeet
people
quotationmark

I suspect you're meant to be using System.Collections.ArrayList which is non-generic - so you'd have: var books = new ArrayList { new Book("Moby Dick", 254) }; Of course, using non-generic collections is generally a bad idea, and unless... more 5/1/2015 9:50:29 AM

people

Add txt files to a runnable JAR file

I'm trying to make a runnable jar file and I'm having problems with my .txt files. My program also have images, but fortunately I've figured out how to manage them. I'm using...
Jon Skeet
people
quotationmark

Yes, if you want to read resources from inside a jar file, you shouldn't use FileInputStream. Perhaps you should add a readResource method: public static Object readResource(Class clazz, String resource) throws IOException,... more 5/1/2015 9:24:28 AM

people