Browsing 7239 questions and answers with Jon Skeet

what causes this error: Sequence operators not supported for type 'System.String'

Here is my code: IEnumerable<DataAccessLayer.SQLPendingEventRecord> dataset1 = _v3Context.SQLPendingEvents .Where(sp => sp.EventType.ToString()...
Jon Skeet
people
quotationmark

I suspect the problem is that it's using Enumerable.Contains(string, char) (because string implements IEnumerable<char>) whereas you really want string.Contains(string). Just changing it... more 3/17/2014 3:57:50 PM

people

what will happen if subclass object is casted to superclass

class Base { void test() { System.out.println("base"); } } public class Derived extends Base { void test() { System.out.println("derived"); } ...
Jon Skeet
people
quotationmark

Casting doesn't change the object at all. The result is just a reference to the same object, but with a different compile-time type. Casting to a super-type can't fail, it can only make a difference to the compile-time type of the... more 3/17/2014 1:53:28 PM

people

StackOverflowError when running a method with switch statement

So for school a task I have is to write a program that converts decimal to binary to hexadecimal. So I'm running into a problem that when I call a method that takes a para of the...
Jon Skeet
people
quotationmark

You're recursing unconditionally: public String getHex(String binary) { StringBuilder hexBuilder = new StringBuilder(); int startIndex = 0; int endIndex = 4; while(true) { ... more 3/17/2014 10:16:58 AM

people

Is it advisable to use MethodBase to find the method names for logging purposes?

I have the following code in almost every function in my source code. string methodName = MethodBase.GetCurrentMethod().Name; My question is Is it recommended to use the above...
Jon Skeet
people
quotationmark

No, I wouldn't do this everywhere in your source code. Firstly, I'd use a dedicated logging package which may have cunning ways of doing a better job, and will certainly be less obtrusive in your source code. There are various options... more 3/17/2014 8:59:42 AM

people

What is the differences between volatile and static keyword?

Static variable keeps single value for a thread but volatile keeps single value for all threads(example here) Most of the scenario constants are declared as static and also...
Jon Skeet
people
quotationmark

I think you've misunderstood both static and volatile. static is just about having a single field across the whole type. It has nothing to do with threads - it's just about whether there's one field for the type (static) or one field for... more 3/17/2014 6:49:01 AM

people

How to implement List.Remove against two different object list?

I have two lists : ListA<A> and ListB<B> Structure of object: Object A : - commonID , AName Object B : - commonID , BName Now my objective is very simple, I'm trying...
Jon Skeet
people
quotationmark

You're currently using Remove (which takes an individual item to remove) instead of RemoveAll (which takes a predicate). However, a better approach might be to create a set of all IDs you want to remove, and then use a single call to... more 3/17/2014 6:43:13 AM

people

Junit: Testing default constructor, dead code warning?

I am attempting to test the default constructor for a class, using Junit: // testing default const public void testCaesarCipher() { CaesarCipher c1 = new...
Jon Skeet
people
quotationmark

Get rid of the check for c1 being null - that's provably impossible. A constructor can never return null. You may want to test that your constructor doesn't throw an exception - in which case just: public void testCaesarCipher() { ... more 3/16/2014 9:36:53 PM

people

Java class doesn't have main method

I need to run a method code for GCD. My java file is called "GCD.java" and the public class is called "GCD." Yet I keep getting the message "Class GCD does not have a main...
Jon Skeet
people
quotationmark

It's entirely valid for a class not to have a main method - or for it to have a main method which isn't declared as public static void main(String[] args). However, in order to treat a class as the entry point for a Java application, it... more 3/16/2014 5:51:09 PM

people

Change ref element value with Array.ForEach?

I'm trying to use the Array.ForEach() extension method to loop through for a list of filtered elements from an array and then modify those values, unfortunately that doesn't seem...
Jon Skeet
people
quotationmark

ForEach simply isn't intended to do this - just like you wouldn't be able to do this with a foreach loop. Personally I'd just use a for loop - it's easy to read and clear: for (int i = 0; i < array.Length; i++) { // Alternatively,... more 3/16/2014 9:14:02 AM

people

How to compare sets stored in IEnumerable?

For the sake of simplicity I pass along sets as IEnumerable. I need to check, whether two such sets are equal. Currently I'm doing it in the following way: if (theirs.OrderBy(t...
Jon Skeet
people
quotationmark

If you could actually keep one of them as an ISet<T>, you can just call SetEquals. You could do this optionally, of course: ISet<Foo> setFoo = theirs as ISet<Foo>; if (setFoo != null && theirs.SetEquals(mine)) { ... more 3/16/2014 8:17:14 AM

people