Browsing 7239 questions and answers with Jon Skeet

.NET, the SqlConnection object, and multi threading

We have an application which uses an SQL Server 2008 R2 database. Within the application, calls to the database are made using a SqlConnection object. This SqlConnection object...
Jon Skeet
people
quotationmark

The obvious solution is to just re-create the SqlConnection object every time a database call requires one - in this case, it would never be shared. Is there any reason not to do this? On the contrary, that's absolutely what you... more 5/29/2014 8:31:02 PM

people

How to decode an utf8 encoded string split in two buffers right in between a 4 byte long char?

A character in UTF8 encoding has up to 4 bytes. Now imagine I read from a stream into one buffer and then into the another. Unfortunately it just happens to be that at the end of...
Jon Skeet
people
quotationmark

You should use a Decoder, which is able to maintain state between calls to GetChars - it remembers the bytes it hasn't decoded yet. using System; using System.Text; class Test { static void Main() { string str =... more 5/29/2014 6:49:52 PM

people

the value of the object is null once I get out of the function (Java)

import java.util.*; public class Algorithm { public static class Matrix{ private Double[][] x; } public static Scanner scan = new Scanner(System.in); private static...
Jon Skeet
people
quotationmark

This is the problem: private static void calc_T(Matrix[] Ts, Double[][] T) { T = Ts[0].x; ... } Arguments in Java are passed by value. So basically, you're ignoring the original value of T, assigning a new value to it, and then... more 5/29/2014 5:59:21 PM

people

Using a generic type as a return type of an async method

A previous question made me wonder why the following method would raise a compile time error: The return type of an async method must be void, Task or Task public async T...
Jon Skeet
people
quotationmark

Three problems: Just because T is "Task or a derived type" doesn't mean that it's Task or Task<T>. What would you expect if I called MyMethodAsync<MyCustomTask> where MyCustomTask derives from Task? The compiler needs to know... more 5/29/2014 5:37:57 PM

people

Changing equality to something else on SQL command

I am working on a windows form application. I want to change the SQL command by user selection. Here is my code: command.CommandText = "SELECT * FROM Product WHERE price =...
Jon Skeet
people
quotationmark

You can't, basically. Parameterized SQL is just for values - not table names, column names, or operators. This is one place where you do probably want to build the SQL dynamically - but with a white-listed set of options. Whether you... more 5/29/2014 5:27:57 PM

people

Method input parameter as namespace

Let's say I have these objects namespace MyResponses.Interfaces.IInterface1 { partial class exResponse { Object1 myObj; bool flag; } } namespace...
Jon Skeet
people
quotationmark

It sounds like this is basically broken by design, if you have very similar types all with the same members (and even the same simple name) but with no type relationship between them. If this is generated code and the types are all... more 5/29/2014 4:56:53 PM

people

Order a list based on a fixed format

int[] OrderedListToFollow = {1,2,4,5} int[] ListB = {2,3,4,8,9} Based on the two lists above I need to sort ListB based on the order defined in OrderedListToFollow. Since 3,8,9...
Jon Skeet
people
quotationmark

As noted, it was already working apart from putting the results at the beginning. To fix this, I'd add an extension method: public static int IndexOfOrMax(this IEnumerable<T> source, T item) { int index = source.IndexOf(item); ... more 5/29/2014 4:37:34 PM

people

4.25.toFixed(1) == 4.35.toFixed(1) == 4.3 but 2.35.toFixed(1)==2.4

When the non fractional part is bigger than 4 the fractional part it is truncated to .3 but when it is smaller than 4 it is rounded to .4. Examples: 1.nr>4: 5.35.toFixed(1)...
Jon Skeet
people
quotationmark

The problem is that the exact values you're calling toFixed on aren't 1.35 etc... they're the nearest IEEE-754 64-bit representation. In this case, the exact values... more 5/29/2014 2:16:42 PM

people

C# and XML: Why is XMLNS attirubute set to all child nodes?

Here is what I have attempted: Creating elements: XmlNode xHeader = xDoc.CreateElement("Customer"); XmlNode xCustomerID =...
Jon Skeet
people
quotationmark

If you want XML of: <Customer_ID xmlns="a"> <Email> </Email> <Home_Address> </Home_Address> <Mobile_Number> </Mobile_Number> </Customer_ID> ... then you need to make sure your... more 5/29/2014 1:12:50 PM

people

Effective use of Collections.unmodifiable*()

What is more preferable way to expose read-only views of collections when considering efficiency, code readability and other criteria? Does it make any real difference from...
Jon Skeet
people
quotationmark

Yes, Collections.unmodifiableList does create a new object each time - but it's a pretty cheap operation. (It doesn't have much work to do.) I'm not sure that the data model of a GUI is likely to fetch the collection over and over again -... more 5/29/2014 6:17:31 AM

people