Browsing 7239 questions and answers with Jon Skeet

C# Data received on UDP is always useless

Here's my problem; I'm trying to make a UDP server - client interaction as a test. I want the client to send data to the server, and if the server matches data the server holds,...
Jon Skeet
people
quotationmark

Firstly, you should add diagnostics to print out the actual data rather than just knowing that it's "right" or "wrong". Next, the actual problem is that you're ignoring the result of udp_sock.ReceiveFrom. That will tell you how many bytes... more 9/28/2013 6:09:01 AM

people

Looking for Task.IsPending

I often do something like this: if (task != null && !task.IsCompleted && !task.IsCanceled && !task.IsFaulted) { // do something, e.g. cancel the...
Jon Skeet
people
quotationmark

I think you're just looking for: if (task != null && !task.IsCompleted) As documented, IsCompleted covers faulted and canceled states as well as RanToCompletion: IsCompleted will return true when the task is in one of the... more 9/28/2013 6:01:44 AM

people

Locking a thread function using Monitor::Enter Monitor::Exit

I have a thread class with two functions: using namespace System::Threading; public ref class SecThr { public: int j; void wralot() { for (int i=0; i<=400000;i++) { ...
Jon Skeet
people
quotationmark

I think you've misunderstood what Monitor::Enter does. It's only a cooperative lock - and as wralot doesn't try to obtain the lock at all, the actions of setalot don't affect it. It's not really clear why you expected to get a constant... more 9/28/2013 5:51:31 AM

people

Visibility of a private inner class of a public class

While this is obviously a RTFM case, somehow I failed to find a concise source explaining it all. public class Outer { private class Inner { } } Private class Inner is...
Jon Skeet
people
quotationmark

The "FM" in this case is the Java Language Specification. You want section 6.6.1 which includes: Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top... more 9/27/2013 10:07:36 PM

people

How to initialize structure with compiler generated backing fields

Ultra simple question... How does one properly initialize private backing fields on a structure? I get a compiler error: Backing field for automatically implemented property...
Jon Skeet
people
quotationmark

This is a problem using automatically implemented properties in structs. You need to explicitly chain to the parameterless constructor: public Rectangle(int x, int y, int width, int height) : this() { X = x; Y = y; Width =... more 9/27/2013 9:31:12 PM

people

How to test properties atomically in nunit

Given an object with several properties, say System.Drawing.Rectangle, I wanted to assert the values of ALL the properties (not stopping when ONE property didn't match) and report...
Jon Skeet
people
quotationmark

I'm assuming you don't want to make the type itself check for equality and override ToString? Because that would do it nice. One option would be to use anonymous types to accomplish the same goal: Assert.AreEqual(new { X = testX, Y =... more 9/27/2013 9:28:18 PM

people

Comparing contents of string array elements with names

I have one method called "PopulateNameArray" and another method called "FindStudentPosition". In FindStudentPosition(string name, string[] array), I have int intLocation =...
Jon Skeet
people
quotationmark

This is already implemented for you, with the Array.IndexOf method: int index = Array.IndexOf(array, name); Or use the fact that an array implements IList<T>, and use IList<T>.IndexOf: // IndexOf is implemented... more 9/27/2013 8:46:50 PM

people

C# How to Initialize Generic class with object of type "Type"

I recently had this problem. doSomething(typeof(int)); doSomething(typeof(MyClassA)); doSomething(typeof(MyClassB)); public void doSomething(Type _type) { var myGenObj = new...
Jon Skeet
people
quotationmark

You want Type.MakeGenericType and then Activator.CreateInstance... but then calling a method on the newly-created object will be tricky. Ideally you could have a non-generic base class or interface containing those members: public... more 9/27/2013 8:14:32 PM

people

How can I show Currency with Negative instead of Parentheis in ASP NET MVC?

I want my values to be shown in the views Like: -$150.00 Instead of: ($150.00) -- I guess this is what I have to do: How do I display a negative currency in red? But I...
Jon Skeet
people
quotationmark

It's all down to NumberFormatInfo.CurrencyNegativePattern. Presumably you've got the value 0, when it sounds like you want 1. It's not clear whether you're currently using the user's CultureInfo, the server's one, or something else. But... more 9/27/2013 7:50:17 PM

people

Java Static Initialization Order

I'm trying to discover the order in which initialization occurs, or rather the reason behind why initialization occurs in this order. Given the code: public class Main { { ...
Jon Skeet
people
quotationmark

I think you're just missing section 12.4.2 of the JLS, which includes: Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though... more 9/27/2013 7:39:42 PM

people