Browsing 7239 questions and answers with Jon Skeet

Is there a reason to use &= rather than just = on a boolean?

I ran across some code today that was checking a number of error conditions. A single boolean was used all the way through, but instead of reassigning it every time with an =, it...
Jon Skeet
people
quotationmark

You're assuming the assertions are enabled. If this is using Debug.Assert, the method will only actually be called if the DEBUG conditional compilation symbol is defined. Assuming result is used later on (beyond the assertion) then... more 10/9/2013 3:14:31 PM

people

matcher (java.lang.CharSequence) in Pattern cannot be applied to (java.io.BufferedReader)

I'm using Regex to match something in a text and I get each line with BufferedReader. Then I find the problem I can't solve: BufferedReader br=null; BufferedWriter...
Jon Skeet
people
quotationmark

Well as it says, you can't apply a Pattern to a BufferedReader - you have to read data from the reader, and then apply the pattern to that. In this case, you're already reading the data - but then you're ignoring it! You want: Matcher m... more 10/9/2013 2:40:55 PM

people

Compile package with java

I have these programs in java: //file ../src/com/scjaexam/tutorial/GreetingsUniverse.java package com.scjaexam.tutorial; public class GreetingsUniverse { public static void...
Jon Skeet
people
quotationmark

You haven't imported the Earth class, so the compiler doesn't know what Earth refers to. You should have this line at the start of your GreeingsUniverse.java file: import com.scjaexam.tutorial.planets.Earth; more 10/9/2013 2:02:09 PM

people

Using ConcurrentBags

I am running some code that uses ConcurrentBags. I am exploring the IEnumerable functionality. The code I run is ConcurrentBag<int> bag = new...
Jon Skeet
people
quotationmark

You have a race condition, basically. On my machine, this does print 42 most of the time - but fundamentally you have two independent tasks: one adding, and one printing. There is no guarantee which task will execute its first statement... more 10/9/2013 1:49:49 PM

people

A field initializer cannot reference the non static field, method, or property 'NecroBomber.Form1.path'

im trying to make something that can read a list off accounts from an online .txt file on my website but i keep getting the error A field initializer cannot reference the...
Jon Skeet
people
quotationmark

To solve the compile-time issue, put the initialization in the constructor. You're simply not allowed to refer to one instance variable from the initializer of another. This is laid down in the C# specification in section 10.5.5.2: A... more 10/9/2013 1:21:12 PM

people

Why can't I add a new pair into a SortedDictionary?

I have this SortedDictionary: var signature_parameters = new SortedDictionary<string, string> { { "oauth_callback", SocialEngine.twitter_aggrega_redirect_uri }, {...
Jon Skeet
people
quotationmark

Where am I wrong? You're calling the Add method, but expecting it to return something. It doesn't. If you're trying to create a new dictionary with the same entries and one new one, you probably want: var headerParameters = new... more 10/9/2013 10:23:12 AM

people

Using AsSequential in order to preserve order

I am looking at this code var numbers = Enumerable.Range(0, 20); var parallelResult = numbers.AsParallel().AsOrdered() .Where(i => i % 2 == 0).AsSequential(); foreach...
Jon Skeet
people
quotationmark

AsSequential is just meant to stop any further parallel execution - hence the name. I'm not sure where you got the idea that it's "supposed to make the resulting array sorted". The documentation is pretty clear: Converts a... more 10/9/2013 7:30:31 AM

people

Can I make the size of a byte array dynamic?

I'm using a byte array with the write() method of OutputStream to store the data into a file. For that, I'm initiating the byte array with a predefined size. But I don't want it...
Jon Skeet
people
quotationmark

The problem isn't on the output side at all - it's just that you don't know how big the input is. It's not clear where the InputStream is coming from - if the input is being received over the network for example, you may not have any... more 10/9/2013 6:18:14 AM

people

Why would ref be used for array parameters in C#?

I read the page Passing Arrays Using ref and out (C# Programming Guide) and was wondering why we would need to define an array parameter as a ref parameter when it is already a...
Jon Skeet
people
quotationmark

Won't changes in the callee function be reflected in the caller function? Changes to the contents of the array would be reflected in the caller method - but changes to the parameter itself wouldn't be. So for example: public void... more 10/8/2013 7:58:22 PM

people

Cross.Thread violation

I have a class with an event and a custom EventArgs. The meaningful code: public void OnTickReceived(TickReceivedEventArgs e) { ...
Jon Skeet
people
quotationmark

I receive InvalidOperationException - cross.thread operation. Where my error? Presumably T3OpenStockReader raises events on its own thread - but you're trying to modify the UI in your event handler... which means you're doing it in... more 10/8/2013 7:21:59 PM

people