Browsing 7239 questions and answers with Jon Skeet

Regarding object = new when another reference type points to it

I am vary curious regarding a specific case in c#: In this code, pointToSourceArr points to source arr, but if I change sourceArr by a setting it to a new array or setting it to...
Jon Skeet
people
quotationmark

Why is that? The value of each variable is just a reference to the array. The assignment here: string[] pointToSourceArr = sourceArr; just copies the existing value of sourceArr into pointToSourceArr. It doesn't set up a... more 11/23/2014 8:49:18 AM

people

Java Sum Of Two Dice Will This Code Give Above A 6?

public class SumOfTwoDice { public static void main(String[] args) { int SIDES = 6; int a = 1 + (int) (Math.random() * SIDES); int b = 1 + (int)...
Jon Skeet
people
quotationmark

No, Math.random() will never return 1. It has an inclusive lower bound of 0, but an exclusive upper bound of 1.0. From the documentation - emphasis mine: Returns a double value with a positive sign, greater than or equal to 0.0 and... more 11/23/2014 8:06:58 AM

people

Preventing object class creation

I've been reading the article about constructors and what have been written there is: A public class can likewise prevent the creation of instances outside its package by...
Jon Skeet
people
quotationmark

You missed this bit: by declaring at least one constructor That's what's preventing the default constructor from being created. The example given has a default access (package access) constructor: PackageOnly() { } ... so it... more 11/23/2014 7:57:02 AM

people

C# cannot implicitly convert type when types are the same

I have a generic class: public abstract class ModelSet<T> : ObservableCollection<T>, IModelObject where T : ModelObject, new(){ public static ModelSet<T>...
Jon Skeet
people
quotationmark

Surely, they are the same. No, they're not the same. One is Movements, and the other is ModelSet<Movement>. While every Movements instance is an instance of ModelSet<Movement>, it's entirely possible to have a... more 11/22/2014 5:49:32 PM

people

Java Multiple Threads Demo not Working

I'm writing a small program to see how multiple threads can be run in Java. Not sure why I'm not getting any output: class NuThread implements Runnable { NuThread() { ...
Jon Skeet
people
quotationmark

You're not creating any instances of NuThread. This line: NuThread t1, t2, t3; ... just declares three variables. It doesn't create any instances. You'd need something like: NuThread t1 = new NuThread(); NuThread t2 = new... more 11/22/2014 5:16:04 PM

people

How do I test out my program in the main method?

This will probably sound like a dumb question to many of you but I'm a new student and I am trying to learn. This is a program that takes a roman numeral input from a user and...
Jon Skeet
people
quotationmark

There are a few changes you need: If you're going to call your getUserInput method from main, you either need to make it static or create an instance of your class. I'd suggest making it a static method. Currently your romanToDecimal... more 11/22/2014 4:12:32 PM

people

LINQ query to search multiple conditions in a specific order?

I have a static set of categories, and an incoming list of items that are in various categories. I want to get the first item that matches the best category, and if none is found,...
Jon Skeet
people
quotationmark

There's certainly a more concise way of doing it, in two ways: Use the overload of FirstOrDefault which takes a predicate Use the null-coalescing operator I'm going to ignore your IsBar check for now, because I don't understand how... more 11/21/2014 11:26:56 PM

people

Filter by time range in list of DateTime objects

I have a list of DateTime objects. I need to take from this list only those objects that have time part of the date between '00:00:01' and '12:00:00'. How can I query this...
Jon Skeet
people
quotationmark

It seems to me that all you need is the DateTime.TimeOfDay property. I would then recommend changing your conditions slightly if you're able to, so that the lower bound is inclusive and the upper bound is exclusive, e.g. var time =... more 11/21/2014 11:14:53 PM

people

Need Help Understanding this generic declaration

Below is a method from java.util.concurrent.ScheduledExecutorService: /** * Creates and executes a ScheduledFuture that becomes enabled after the * given delay. * * @param...
Jon Skeet
people
quotationmark

Why is there <V> ScheduledFuture<V>? Because that's the type parameter and the return type. The <V> part isn't a return type, it's just saying "This is a generic method with a single type parameter, V." So we... more 11/21/2014 9:34:54 PM

people

If Enum Has specific flag, check to see if it has any others

I have a rather large Flag enum called AmendmentType. I need to check to see that if it has specific Enums and any others. For Example: var foo = AmendmentType.Item1; if...
Jon Skeet
people
quotationmark

If you're only interested in the part you've labeled as "anything else", you can use: if (foo.HasFlag(AmendmentTypeEnum.Item1) && (foo & ~AmendmentTypeEnum.Item1) != 0) Or just check that it isn't exactly equal to Item1: if... more 11/21/2014 9:22:27 PM

people