Browsing 7239 questions and answers with Jon Skeet

In Java: UDP based Client/Server not giving the expected output

I have written a simple Client/Server code in Java, in which a client sends a message to server (which is displayed on Server's Standard Output) and then server also sends a...
Jon Skeet
people
quotationmark

You never populate the packet with any useful data: byte[] buf = new byte[256]; String msg = stdIn.readLine(); packet = new DatagramPacket(buf, buf.length, address, 4445); socket.send(packet); You've just posted a byte array with 256... more 10/16/2014 7:45:01 PM

people

Monitor.Enter: No overload for method takes 2 arguments

I recently inherited some code that I can't get to build yet. Here's the offending line: Monitor.Enter(this.foo, ref lockTaken); According to Visual Studios there is only one...
Jon Skeet
people
quotationmark

That overload was introduced in .NET 4. So as you were targeting .NET 2.0, it wasn't present. When in doubt, check the "Version information" section at the bottom of the documentation. Admittedly I'd normally be at least slightly nervous... more 10/16/2014 6:42:34 PM

people

Class Names in C# (English vs Intellisense)

I'm using Visual Studio 2013 to write C# code. How should I name my classes? In a "English-friendly" way, or in a way thats more IntelliSense- friendly. For instance, I have a...
Jon Skeet
people
quotationmark

It usually makes sense to put the differentiator at the start. For example: TextReader / StreamReader / StringReader Stream / FileStream / MemoryStream / NetworkStream It's like having an adjective to provide more detail: "the red... more 10/16/2014 5:39:54 PM

people

Is a readonly EnumSet iterator thread safe?

I have an EnumSet which is final and immutable i.e. initialized once in the constructor. Is the contains() method on this EnumSet thread safe? It is internally using an iterator...
Jon Skeet
people
quotationmark

No, if two threads call contains() at the same time, that will call iterator() twice which will create two separate iterators. If you were trying to share an iterator between two threads, that would not be a good idea. Note that if you... more 10/16/2014 4:59:44 PM

people

How do I use high order unicode characters in java?

How do I use unicode characters in Java, like the Negative Squared Latin Capital Letter E? Using "\u1F174" doesn't work as the \u escape only accepts 4 hex-digits.
Jon Skeet
people
quotationmark

You need to specify it as a surrogate pair - two UTF-16 code units. For example, if you copy and paste the character into my Unicode explorer you can see that U+1F174 is represented in UTF-16 code units as U+D83C U+DD74. (You can work... more 10/16/2014 3:12:55 PM

people

How to safely convert a string containing escaped JSON to valid JSON?

I am communicating with a third party API that returns JSON responses as follows: "{\"SomeResponse\":{\"FIrstAttribute\":8,\"SecondAttribute\":\"On\",\"ThirdAttribute\":{\"Id\":2,\"FirstName\":\"Okkie\",\"Name\":\"Bokkie\",\"Street\":\"\",\"StreetNumber\":null,\"PostCode\":\"\",\"City\":\"\",\"Country\":\"}}}" It is kind of JSON... but as a String. Note the first and ending double quotes and of course all the escape slashes. Currently, I solve this by String.Replacing the backslashes and the first and end quote. After that, I am able to parse it. mystring.Replace("\\", ""); However, what if one of the attributes actually has an backslash as a value? For example: \"SecondAttribute\":\"My Super Back Slash: \\ . That was it.\" In that case, I would accidentally remove the backslash that should be there in the value. Does anyone have a bright idea on how to parse this JSON String properly?
Jon Skeet
people
quotationmark

This is basically JSON encoded as a JSON string - after doctoring the end of your string very slightly, as per comments. It's not too hard to handle that in Json.NET, using JToken.Parse to effectively unescape first, then parsing the... more 10/16/2014 2:06:58 PM

people

Creating variable of type <derived class> to store <base class> object in C#

i have a question about practical use of this example, when you are creating a variable of type derived class to store a variable of base class object. DerivedClass C = new...
Jon Skeet
people
quotationmark

in what case i can use this. You can't. Ever. The main point of static typing is so that you can rely on the value of a variable being compatible with the type you've declared. So if you declare a variable as: DerivedClass C; then... more 10/16/2014 11:55:56 AM

people

Grouping Linq Issue, can't get it right

public class Emp { public int EmpId { get; set; } public string Type { get; set; } public List<string> Email { get; set; } } I fetch data from...
Jon Skeet
people
quotationmark

Your group emp.Email by new { emp.EmpId, emp.Type } means that each element of the group will have a key of the anonymous type, and an "element type" of List<string>. You're then propagating that element type using Emails =... more 10/16/2014 10:42:23 AM

people

Get another attribute on the same element by searching another attribute

This might be the simplest question for that i appologise. This is part of my xml. What i want to get is the attribute "Value" based on another attribute "ItemOID". IE when...
Jon Skeet
people
quotationmark

It looks to me like you want a new row for each ItemGroupData element, not each ItemData element. I would expect something like: foreach (var itemGroup in doc.Root.Elements("ItemGroupData")) { var row = patDt.NewRow(); ... more 10/16/2014 9:39:32 AM

people

Merge two XML files, files having one to many relationship C#

I have two XML files, 1. Contracts <?xml version="1.0" encoding="UTF-8"?> <File> <Contract> <ContractNo>1</ContractNo> ...
Jon Skeet
people
quotationmark

I would personally read in the assets, populating an ILookup<int, XElement> and removing the ContractNo element afterwards (as it's just slightly simpler in LINQ to XML). Then read the contracts, populating the assets from the... more 10/16/2014 6:00:33 AM

people