Browsing 7239 questions and answers with Jon Skeet

LINQ to XML aggregates and group by

I'm Attemping to do a SUM and a Group By in LINQ to XML. I'm also using LINQPad 4 to test my linq out. For some reason rate = g.Sum(taxRate) is giving me a FormatException error....
Jon Skeet
people
quotationmark

Okay, given the "tax rate of an empty string should be treated as 0", I would probably break that up into a separate method. It's simpler than trying to do it inline. Then I'd use explicit conversions everywhere. So something... more 2/5/2014 8:12:04 PM

people

One class included to other class by composition. How to change it's private fields?

I have simple class: public class Points { private int x = 0; public void setX(int x) { this.x = x; } } And I make other class which have a Points...
Jon Skeet
people
quotationmark

I think, this code have a doubling: if I change a method name in Points I also have to change method setX in class Curve. No you don't. You can if you want, but the two methods are independent. You don't have to expose all the methods... more 2/5/2014 7:30:10 PM

people

How do you set a value in a ConcurrentDictionary regardless of whether it contains the Key

First of all, is it safe to simply add an item to a concurrent dictionary using the indexed assignment (e.g. myConcurrentDictionary[someKey] = someValue;)? I'm just confused...
Jon Skeet
people
quotationmark

Yes, using the indexer should be absolutely fine if you want to add or replace a value. AddOrUpdate takes a delegate so that you can merge the "old" and "new" value together to form the value you want to be in the dictionary. If you don't... more 2/5/2014 6:03:14 PM

people

JUnitParams not working with String array

Consider this test class, working with JUnit 4 and JUnitParams: import static junitparams.JUnitParamsRunner.$; import junitparams.JUnitParamsRunner; import...
Jon Skeet
people
quotationmark

Okay, basically it looks like it's a bug in JUnitParams. After applying the fix in the original answer below, the library still "unwraps" the String[] when we don't want it to - given the amount of conditional wrapping and unwrapping going... more 2/5/2014 4:24:44 PM

people

Avoiding debugger warning in IsNothing() statement

I am using Visual Studio 2012 and have the following code block. The code checks the file type of a specific file (that's why there are return True/False in it). If it encounters...
Jon Skeet
people
quotationmark

That's why I have the IsNothing statements That suggests you expect the values to be IsNothing before they're given a specific value. In C#, this wouldn't just be a warning - it would be an error. I have two suggestions: If you... more 2/5/2014 1:02:59 PM

people

CSharp > Property getter expression does not work with Condition

I've got the following Code: public static Func<object, string> GetPropGetter(Type objectType, string propertyName) { ParameterExpression paramExpression =...
Jon Skeet
people
quotationmark

It may not be the only thing that you need to do, but I think you want to use Expression.Condition rather than Expression.IfThenElse. Currently you've got the equivalent of: if (condition) { default(...); } else { ... more 2/5/2014 10:52:23 AM

people

Encountered an IOException reading from InputStream in Java

I am running these lines of code in a Java program FileInputStream in = new FileInputStream(file); InputStreamReader reader = new InputStreamReader(in); ...
Jon Skeet
people
quotationmark

I suspect the problem is that you're setting the length based on the length of the file in bytes, whereas the parameter to setCharacterStream is meant to be a length in characters. You're also using the platform default encoding, which is... more 2/5/2014 7:56:16 AM

people

Garbage Collector, call & callvirt and Debug/Release code mode execution differences

I have a class: public class SomeClass { public int I; public SomeClass(int input) { I = input; Console.WriteLine("I = {0}", I); } ~SomeClass()...
Jon Skeet
people
quotationmark

I doubt that it's really anything to do with call and callvirt. I strongly suspect it's simply because you're not using any fields within SomeClass.Foo. The garbage collector is free to collect an object when it is certain that none of... more 2/5/2014 7:43:53 AM

people

How to sort ArrayList<String> containing elements of ArrayList<HashMap<String, String>> that has been sorted by a comparator?

I am getting some data via JSON which I store in a Hashmap map e.g. map.put(KEY_TITLE, parser.getString("title")) and then add the map to an ArrayList bookList. This is displayed...
Jon Skeet
people
quotationmark

Right, with the new code it seems reasonably simple: just create the individual lists after you've sorted the list of maps: // This loop looks wrong at the moment - you're not using i for (int i = 0; i < json.Length(); i++) { ... more 2/5/2014 7:15:20 AM

people

SQL order by [Order] but group similar items together

Sorry if I don't really know the terms for this, I will try to explain this the best I can... This is what I've got now SELECT * FROM Products ORDER BY [Order] Order ...
Jon Skeet
people
quotationmark

It nearly sounds like you don't really want it sorted by order, primarily - you want it sorted by product ID and then order. So: SELECT * FROM Products ORDER BY ProductId, [Order] And in a LINQ query expression: var results = from... more 2/5/2014 7:05:03 AM

people