Browsing 7239 questions and answers with Jon Skeet

Declaring variables slow down program

I have a program which uses a RichTextBox (which is part of Tab Control) and in the TextChanged event I these variable declarations: RichTextBox programTextBox =...
Jon Skeet
people
quotationmark

It's not the variable declarations - it's the code you're using to initialize them. I wouldn't be at all surprised to find that GetLineFromCharIndex and GetFirstCharIndexFromLine were expensive - and currently you're calling the Lines... more 5/23/2015 7:39:56 PM

people

The received file is larger then the sent file

This is the code that sends the file: byte[] data = FileUtils.readFileToByteArray(new File("my_file.docx")); System.out.println(data.length); // prints...
Jon Skeet
people
quotationmark

It's not clear exactly what's actually sending the file, but it's being transmitted as base64 - which is why it's 4/3 the size of the original. I'm pretty sure if you run the received file through a base64 decoder you'll get the original... more 5/23/2015 7:36:56 PM

people

Item from IEnumerable changed inside foreach, but change not saved to collection

I had an interesting problem today, this is a reproduction: class TestListAndEnumerable { public static void Test() { bool b1 = GetBool1(); // returns false ...
Jon Skeet
people
quotationmark

This is a matter of LINQ's lazy evaluation. Each time you iterate over list in GetBool1(), you're creating a new sequence of values. Changing the Value property in one of those objects doesn't change anything else. Compare that with... more 5/23/2015 1:50:52 PM

people

diffrent way Define a List of Objects in C#?

I have written a program that is supposed to get a list of names and information. Well, I got to solve this question a few classes, such as students and university have to answer....
Jon Skeet
people
quotationmark

You're never initializing your student field, so it's null. You could use: // Note: public fields are almost *always* a bad idea private readonly Student[] students; public University(int numberOfStudents) { students = new... more 5/23/2015 1:23:16 PM

people

Converting List of childs to List of parents in one line

Converting List of Banana to List of Fruit ... public class Fruit { } public class Banana extends Fruit { } public List<Banana> allBananas() { return new...
Jon Skeet
people
quotationmark

It really depends on what you want to achieve. If you're happy to create a copy of the list, then I'd just use something like: public List<Fruit> getFruits() { return new ArrayList<>(allBananas()); } That's now... more 5/23/2015 9:20:12 AM

people

Generic array index out of bounds

Ok , here is my problem . I'm learing to use generic classes and methods. I want to make an generic array list and method that will add/remove element by choosen index. I simply...
Jon Skeet
people
quotationmark

You're creating an empty ArrayList, and then trying to get the third element (element at index 2) from it within your push method. That's not going to work. Now, you're currently ignoring your initSize parameter in your constructor. You... more 5/23/2015 8:58:00 AM

people

why am i appending twice in jtextfield?

I have looked over this code and i don't know what is wrong. I keep getting incorrect outputs when i enter a command (any input). Please look at the bottom part of my...
Jon Skeet
people
quotationmark

The problem is that you're abusing the threading model. You shouldn't be accessing UI components in a thread other than the UI thread - and having a tight loop like this is pretty much always a bad idea. You should read about the Swing... more 5/23/2015 8:55:22 AM

people

Is it possible to store functions in a dictionary?

I have a message coming into my C# app which is an object serialized as JSON, when i de-serialize it I have a "Name" string and a "Payload" string[], I want to be able to take the...
Jon Skeet
people
quotationmark

It sounds like you probably want something like: Dictionary<string, Func<string[], int>> functions = ...; This is assuming the function returns an int (you haven't specified). So you'd call it like this: int result =... more 5/22/2015 1:26:00 PM

people

Short Circuit AND, OR and Precedence Table

Hello i have a question regarding precedence Table in java. It says && has a higher precedence over ||. boolean b,c,d; b = c = d = false; boolean e = (b = true) || (c =...
Jon Skeet
people
quotationmark

The precedence here just means that X || Y && Z is equivalent to: X || (Y && Z) That executes as: Evaluate X If X is true, the result is true Otherwise, evaluate Y If Y is false, the result of Y && Z is... more 5/22/2015 12:31:32 PM

people

Сircumvent the limitation of c# and pass collection of child elements

In my Windows Store App I used c# 5.0. And I need to call the method which takes collection of base class, by passing collection of child class: public class Foo // base...
Jon Skeet
people
quotationmark

It looks like DoSomething(List<Foo> foos) only actually needs to iterate over the list. So you can change it to: public void DoSomething(IEnumerable<Foo> foos) { // Body as before } Now you can pass in a List<Bar>... more 5/22/2015 12:22:02 PM

people