Browsing 7239 questions and answers with Jon Skeet

Decimal to String "Specified cast is not valid"

private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) { SqlCommand cmd = new SqlCommand(@"Select * FROM PRODUCTS where [PRODUCT CODE] = '" + comboBox4.Text +...
Jon Skeet
people
quotationmark

It sounds like you probably just want: decimal price = reader.GetDecimal(reader.GetOrdinal("PRICE")); textBox7.Text = price.ToString("0.00"); Basically, decimal and int aren't the same things, and if you're storing a value as a decimal,... more 3/3/2016 1:21:01 PM

people

Parsing XML excluding elements

I'm trying to parse a xml file, which isn't the problem. But my xml file has multiple <name> tags. One is for a song, and the other one is for an artist. I want to exclude...
Jon Skeet
people
quotationmark

It sounds like you just want to use Elements instead of Descendants, but at the right point - it's not clear where you tried using it. I'd also recommend using ToList to make things simpler. Given the sample in the documentation, it looks... more 3/3/2016 8:41:19 AM

people

Converting UTC date time to a time in the future, considering daylight savings time of a time zone

We have a system that represents weeks with UTC times representing begin & end date times from the America/Chicago time zone. Weeks start at midnight on Saturday morning...
Jon Skeet
people
quotationmark

Okay, if I've understood you correctly, I think you probably want something like: var zone = DateTimeZoneProviders.Tzdb["America/Chicago"]; var instantStart = Instant.FromDateTimeUtc(week.BeginDate); var chicagoStart =... more 3/2/2016 3:34:13 PM

people

How to Create a Method with 'new' keyword in its method signature using codedom

I want to dynamically generate a method with 'new' keyword in its method signature using codedom. Want to generate something like this private new void MyMethod() { }
Jon Skeet
people
quotationmark

When you call TypeBuilder.DefineMethod, just include MethodAttributes.NewSlot: Indicates that the method always gets a new slot in the vtable. more 3/2/2016 1:48:14 PM

people

c# linq delete multiple nodes

I'm trying to remove multiple nodes that a particular element(path) contains a value but I'm receiving a System.NullReferenceException any help where I'm going wrong would I'be...
Jon Skeet
people
quotationmark

I'm not sure why you're getting the exception, but I strongly suspect it's because you're modifying the document while you're querying it. If you change your code to use a ToList() call to get the list of nodes to remove, that doesn't... more 3/2/2016 9:26:55 AM

people

Faster when I save the result of a method in a new variable?

I was wondering if it is any helpful helpful for the performance (next to the readability) when I save the result of a method in a new variable for further uses. My thought was,...
Jon Skeet
people
quotationmark

If you mean you're trying to avoid getHeaderByteArray from being called multiple times, then yes, you're doing exactly the right thing. The "cost" of having one extra local variable is negligible in almost all cases. I'd say it's also more... more 3/2/2016 8:18:09 AM

people

Why does GregorianCalendar.getTimeInMillis() return wrong value?

I want to calculate time gap in form of days between two dates. So I wrote following code: public static int calculateTimeGap(long start, long end) { GregorianCalendar...
Jon Skeet
people
quotationmark

You can see that two GregorianCalendars present different days, but finally we got a gap equals 0. Yes, and that's entirely reasonable. You're comparing March 2nd, 15:28:08 with March 3rd, 01:00:00. They're on different dates, but are... more 3/2/2016 7:58:39 AM

people

String stops appending after char 00H

I am doing some experiments and as part of the exploratory process I am trying to write a program that would present files in a similar fashion a hex editor would do. I load a...
Jon Skeet
people
quotationmark

Next to each line I want to write the bytes in ASCII format but when I encounter 00H all the text after that is not appended to the string. Yes it is - it's just not shown in the text box. (You're also using ISO-8859-1, not ASCII.... more 3/2/2016 6:43:15 AM

people

Why does Number() return wrong values with very large integers?

Number() function returns incorrect values on some arguments, like this: Number('10000000712224641') returns 10000000712224640 Number('10000000544563531') returns...
Jon Skeet
people
quotationmark

Javascript uses 64-bit IEEE-754 binary floating point to store all numbers - like double in C# and Java, for example. There isn't a different type to store integers. (The actual implementation may use optimizations to avoid always... more 3/1/2016 4:11:00 PM

people

Combining tasks results in a dictionary

I'm trying to parallelize work that relies on external resources, and combine it into a single resulting dictionary. To illustrate my need, imagine I want to download a set of...
Jon Skeet
people
quotationmark

You need to perform the mapping yourself. For example, you could use: static async Task<Dictionary<string,string>> GetUrls(string[] urls) { var tasks = urls.Select(async url => { using (var client = new... more 3/1/2016 2:16:35 PM

people