Browsing 7239 questions and answers with Jon Skeet

Distinct clause causing type error

Trying to do a .Distinct() clause in ASP.net MVC Entity Framework, but getting an error on the following code: var tblGlobalLogOnLogOffStudentBans =...
Jon Skeet
people
quotationmark

You're selecting the user ID (resulting in a sequence of user IDs) and then calling Distincton that. The result is still a sequence of user IDs... whereas it looks like your page wants a sequence of tblGlobalLogOnLogOffStudentBan. I... more 4/16/2015 10:20:03 AM

people

How to override base classes\structs such as int, string?

To reduce downvotes: (Skippable at first) I am aware that this question sounds pointless and\or weird. I am creating JIT that takes C# code compiles it with csc.exe, extracts...
Jon Skeet
people
quotationmark

Even a way to reach to csc.exe source code may help. (This is critical.) Judging by your comments, this is actually the bit you really need. (Trying to change int and string themselves would involve changing mscorlib and almost... more 4/16/2015 10:13:24 AM

people

Why does this if statement, with an assignment and equality check, evaluate to false?

How does a Java if statement work when it has an assignment and an equality check OR-d together?? public static void test() { boolean test1 = true; if (test1 = false ||...
Jon Skeet
people
quotationmark

This is a precedence issue, basically. You're assuming that your code is equivalent to: if ((test1 = false) || (test1 == false)) ... but it's not. It's actually equivalent to: if (test1 = (false || test1 == false)) ... which is... more 4/16/2015 9:28:33 AM

people

Java prevent unused warnings

So i was using Eclipse to create a simple program, in this program I have a object that has no other methods other than the constructor. Something like this class...
Jon Skeet
people
quotationmark

The whole logic of Example class is in the constructor That sounds like a poor design to start with. If you don't really need an instance, why create it? It sounds like you should probably just put the code from the constructor into a... more 4/16/2015 7:51:13 AM

people

c# switch on variable type

Is there a cuter way to do this? Given a byte stream to convert it to the desired number type. (Assume the calling code will be handling data types relevant to the number of...
Jon Skeet
people
quotationmark

Well you can extract the conditionality of the array reversal, and I wouldn't use overloads at all: public ushort GetUInt16(byte[] bytes) { ReverseIfLittleEndian(bytes); return BitConverter.ToUInt16(bytes, 0); } public uint... more 4/15/2015 4:18:40 PM

people

Avoid xml escaping of angle brackets, when passing xml string to System.Xml.Linq.XElement

I'm getting a string from string GetXmlString(); this I cant change. I have to append this to an xml within a new XElement ("parent" , ... ); , to the ... area. This string I'm...
Jon Skeet
people
quotationmark

The simplest option is probably to give it a root element, then parse it as XML: var doc = XDocument.Parse("<parent>" + text + "</parent>"); If you need to append to an existing element, you can use: var elements =... more 4/15/2015 6:31:50 AM

people

Use a Dictionary for creating new instances of objects

I wonder if it is possible to use Dictionary<string, MyType> as a object factory, in other words, given a string "Foo" I want to retrieve a new instance of my Foo...
Jon Skeet
people
quotationmark

I wonder if it is possible to use Dictionary as a object factory, in other words, given a string "Foo" I want to retrieve a new instance of my Foo class. Given then that you want to create a new instance, I would suggest using a... more 4/15/2015 6:15:30 AM

people

How To parse XML and JSON data in single Response in Android

Can Any one help me to parse XML and JSON data response in Android. From my web service i got following type of response. i am bit confused as to how can i parse this data . Due...
Jon Skeet
people
quotationmark

It looks like that's just XML wrapping JSON. So parse it as XML, then take the text content of the root element (the only element present) and then parse that as JSON. Basically, just do one thing at a time and you'll be fine: treat the... more 4/15/2015 6:05:32 AM

people

How to avoid reference comparison with self implemented value types

I'm trying to implement a value type that in general mimics the behavior of type short. So far comparison and assignments between my value type and short are working out fine,...
Jon Skeet
people
quotationmark

You can't do this. Basically, there's no way of symmetrically implementing Equals between two types unless they know about each other. Note that this assertion: Assert.IsTrue(SHORT_TYPE.Equals(valueType)); works due to the implicit... more 4/14/2015 5:54:30 PM

people

Can a collection be modified by another thread during ToList()

class Sample { private List<int> _list; public List<int> List { get { return _list.Select(p => p + p).Where(q => q % 2...
Jon Skeet
people
quotationmark

There's no multi-threading protection in List<T>, so yes, another thread could modify the list during a ToList call. The results could be: Everything's fine, and the resulting list has the new value Everything's fine, and the... more 4/14/2015 5:34:08 PM

people