Browsing 7239 questions and answers with Jon Skeet

Get Access to XMLNode

i want to read some values of an XML File. I'm able to read some values, but I want to get the Information between the <tools></tools>-tags. XmlNodeList xnList =...
Jon Skeet
people
quotationmark

I strongly suspect this is the problem: XmlNodeList testNodeList = xnc.SelectNodes("/tools"); The leading slash is taking it back up to the root node - you only want to look for tools elements under xnc: XmlNodeList testNodeList =... more 8/26/2014 6:06:54 PM

people

A little bit of string cast

Update: When a database returns a value through the scalar, it will always be an object. So when the value is returned you should have a preconceived notion of what the type...
Jon Skeet
people
quotationmark

No, it doesn't mean either of those - it simply means calling ToString() on whatever the value is. In particular, if the value is not a string, it will convert it to a string anyway - whereas string would fail with InvalidCastException... more 8/26/2014 5:29:20 PM

people

Encapsulating an event handler

I have developed an event handler that monitors a serial port and parses the bytes returned to get a temperature value. My question is, how do I get this value out to the rest of...
Jon Skeet
people
quotationmark

It sounds like you should probably expose another event that the rest of your system could subscribe to - TemperatureChanged, for example. Then you make your event handler (which is attached to the serial port) raise the TemperatureChanged... more 8/26/2014 1:12:29 PM

people

Merge 2 lists with linq where at least one property has changed

var usersOld = new List<User>(); var userOld1 = new User { IsDisabled = true, IsLicenced = false, Id = 1 }; var userOld2 = new User { IsDisabled = true, IsLicenced = false,...
Jon Skeet
people
quotationmark

As Mephy says, implementing IEquatable<User> would make this simpler - at that point, you could just perform a join: var changes = usersOld.Join(usersNew, o => o.Id, n => n.Id, (o, n) => new {... more 8/26/2014 1:09:18 PM

people

Conditions in Ternary Operator Java

I have a simple if/elseif condition which I'm trying to convert it into a return statement with Ternay Operator for code redundancy but I have not been able to. Any help would be...
Jon Skeet
people
quotationmark

There's no need for a conditional operator here. Your code will return true so long as val is neither b nor t: return !(val.equals("b") || val.equals("t")); or: return !val.equals("b") && !val.equals("t"); The first condition... more 8/26/2014 9:12:08 AM

people

Why the numeric promotion of integer and double operands not happen?

In my recent work, I met a question, which is about the numeric promotion of some operands. The following is the demo code: int num1 = 9999999; int num2 = 65536; double result =...
Jon Skeet
people
quotationmark

No, the result of num1 * num2 is promoted to double. Your statement is equivalent to: double result = (num1 * num2) * 1.0; So with promotion, that would be: int firstMultiplication = num1 * num2; double promoted =... more 8/26/2014 9:01:01 AM

people

How to calculate sum of amount for each 5 weeks before in linq?

I am having the amount field in income table in database, as well as the created date in same table. I need data like, Week 1 => Sum(amount for week 1) Week 2 => Sum(amount...
Jon Skeet
people
quotationmark

You can use the methods in EntityFunctions to perform date and time arithmetic. So you should start by working out the start and end dates, then use TruncateTime if necessary to truncate your created date to a date (instead of date and... more 8/26/2014 6:17:42 AM

people

Convert Sbyte to integer in c#

I can't seem to figure out a way to convert an sbyte back to original integer value. int k = 148; sbyte t = (sbyte)k; int asd = Convert.ToInt32(t); The value of asd is -108....
Jon Skeet
people
quotationmark

Assuming you want the result to be in the range 0-255 inclusive, the simplest approach is: int asd = t & 0xff; This will promote k to an int, then take just the bottom 8 bits, leaving all the top 24 bits as 0. However, I'd question... more 8/26/2014 5:53:15 AM

people

Error; Could not find or load main class (Java using Windows CMD)

I am trying to compile and run some java files I have made in Eclipse. The full path to the .java file is C:\Users\MYNAME\Documents\Java\Introduction\src\tests\Test.java. tests is...
Jon Skeet
people
quotationmark

However, I want to simply be able to type java Test That will only work if Test is in the default package - it's as simple as that. You need to pass the java executable the fully-qualified name of the class you want to launch. There's... more 8/25/2014 7:25:02 PM

people

Simplify circular generic constraints in C#?

Given the following base classes: public abstract class PurchaseSystemControllerBase<TController, TViewModel> : IInitializable where TController :...
Jon Skeet
people
quotationmark

No, unfortunately not - at least, not that I'm aware of, without losing some type safety. I have a very similar set-up in my Protocol Buffers port, between the message type and its corresponding builder type. If you'd be happy declaring... more 8/25/2014 7:11:48 PM

people