Browsing 7239 questions and answers with Jon Skeet

Conditional Addition of elements in to a list in C#

I have a multi-dimensional array of double dSet[,] where each row has 1500 elements. I have a List<int> index, with say 100 integers of values (0 - 1499) . Now, I have to...
Jon Skeet
people
quotationmark

Sure - that's just a projection from each element of the index: List<double> val = index.Select(i => dSet[0, i]).ToList(); Any time you've got something along the lines of "for each element from source, do something to get an... more 6/11/2014 6:07:29 AM

people

How to count certain characters in c#?

Im new to C# and I need to make a program that can count these characters (š, ž, č) in a textbox. I put the replace method in but it says No overload for method "Replace" takes 3...
Jon Skeet
people
quotationmark

Replace is used to replace one character or string within a string with another. For example, "mum".Replace("u", "o") will return "mom". That's not counting occurrences of anything - it's not the method you want at all. It sounds like you... more 6/11/2014 5:49:10 AM

people

C# is not able to convert my Character to String in a Foreach loop

I am using a simple code to read the JSON data. I previously needed some help in that, but I am able to overcome the exception through a Google search about the error. But this...
Jon Skeet
people
quotationmark

You're using foreach on a string. Effectively: string x = "foo"; foreach (string y in x) That makes no sense - a string is a sequence of characters, not a sequence of strings. In order to get the code to compile, you could just remove... more 6/10/2014 6:47:13 PM

people

mutate array in JAVA

public class example { public static void main(String[] args) { int[] MyArray = new int[10]; MyArray[1] = 5; int[] NewArray = MyArray; ...
Jon Skeet
people
quotationmark

It seems like whatever changes we made to element in NewArray, MyArray will change along, why? You aren't changing NewArray. You're changing the array that the value of NewArray refers to. It's like this: NewArray is a variable (an... more 6/10/2014 5:26:54 PM

people

When should I separately implement IEnumerator<T>?

In the framework classes of collections I have often seen IEnumerator<T> separately implemented as an inner class and an instance of it is returned in the GetEnumerator...
Jon Skeet
people
quotationmark

Just to answer one part: List<T> has its own enumerator implementation for two reasons: It can't just return the iterator of its backing array, because: It needs to detect structural changes in the list (additions and removals)... more 6/10/2014 3:45:02 PM

people

Optional parameters “must be a compile time constant” with a const still bugging

I've read both Optional parameters "must be a compile-time constant" And Default parameter for value must be a compile time constant? But this keeps not compiling...
Jon Skeet
people
quotationmark

The code you've given has two problems: Optional parameters must come last (apart from params parameters) const fields must be assigned compile-time constants, and string.Empty isn't a const field Because Empty isn't a valid const, your... more 6/10/2014 3:31:45 PM

people

Determine Object that Invokes an Event Handler

I am writing a program in Java that has an array of JButtons and they all need to use the same event handler. The problem is that the event handler needs to make changes to each...
Jon Skeet
people
quotationmark

That's precisely what the source of the ActionEvent is for: public void actionPerformed(ActionEvent e) { JButton button = (JButton) e.getSource(); System.out.println("Event triggered by " + button.getText()); } more 6/10/2014 12:28:49 PM

people

xpath query to select element where attribute not present

What should be xpath query to select an element where the attribute att not present. <root> <elem att='the value' /> <elem att='the value' /> <elem...
Jon Skeet
people
quotationmark

You can use [@att] to test for the presence of an attribute, so you just need: //elem[not(@att)] ... to test for the absence of it. (Tested using xpathtester) more 6/10/2014 12:22:03 PM

people

How to use SkipWhile with multiple conditions

I'm trying to filter objects with SkipWhile but it does not evaluate multiple conditions. Below is a sample code demonstrating the issue, int[] numbers = { 1, 2, 3, 4, 5 }; var...
Jon Skeet
people
quotationmark

It is evaluating both conditions - but as soon as the condition is false, the rest of the sequence is returned. As soon as n==2, n < 2 && n != 2 is false. In fact, your condition makes no sense anyway - if n is less than 2 it... more 6/10/2014 11:13:23 AM

people

How to match Enum Objects with their associated constant values?

I created this enum: public enum CoffeeSorts { Coffee("Kaffee"), Espresso("Espresso"), Mocca("Mocca"), Cappuccino( "Cappuccino"), LatteMacchiato("Latte...
Jon Skeet
people
quotationmark

Your type variable is a String, but you're trying to specify values which are CoffeeSort values. You'll need to convert the String to a CoffeeSort first, or change the signature. For example: public ACoffee createCoffee(CoffeeSort type)... more 6/10/2014 10:42:06 AM

people