Browsing 7239 questions and answers with Jon Skeet

Unable to convert System.DBNull

I have a problem when reading data from excel. If cell is empty, I get exception "Unable to cast object of type 'System.DBNull' to type 'System.String'" I've researched...
Jon Skeet
people
quotationmark

You're calling record.GetString, and I'd expect that to fail, because that's trying to perform the conversion to a string. You should be able to see that in the stack trace. Instead, use record.GetValue: string foo =... more 8/11/2015 9:18:34 AM

people

How to process file line by line in multiple threads and one thread for each line in c# .net?

I've created a c# WPF project, I've to process a csv file having some records which may not be limited to few hundreds or few thousands or millions. I need to read the line of...
Jon Skeet
people
quotationmark

There are two options that can help you here: Parallel LINQ TPL Dataflow Parallel LINQ is the simpler option, but provides a lot less customization. It would look something like: var results = File.ReadLines("input.csv") ... more 8/11/2015 7:16:28 AM

people

Dictionary : Will calling .Keys be ordered consistently with .Values?

Suppose I have a dictionary, and call .Keys, and then .Values with no intervening operations. Will the ordering of the keys be consistent with the ordering of the values? Put...
Jon Skeet
people
quotationmark

Will the ordering of the keys be consistent with the ordering of the values? Yes, it's guaranteed by the documentation of both IDictionary<,>.Keys and IDictionary<,>.Values. Keys documentation: The order of the keys... more 8/10/2015 5:24:08 PM

people

Accessing elements of types with indexers using expression trees

Suppose I have a type like this: class Context { SomeType[] Items { get; set; } } I want to be able to access specific Items elements using expression trees. Suppose I need...
Jon Skeet
people
quotationmark

An indexer is really just a property with an extra parameter, so you want: var property = Expression.Property(ctxExpr, proInfo); var indexed = Expression.Property(property, "Item", Expression.Constant(0)); where "Item" is the name of... more 8/10/2015 4:48:52 PM

people

Java 8 Map.Entry comparator

I've been trying to make Map.Entry Comparator in Java8 using lambda expressions and have found a very strange behaviour. Map<Integer, String> map = new...
Jon Skeet
people
quotationmark

Currently you're specifying the raw type when you specify the method reference - in the first case generic type inference with the assignment tells the compiler what you mean, but that doesn't work for the target of a method call. You can... more 8/10/2015 1:20:47 PM

people

Java BigInteger bitLength() method ignores leading 0 bits

Java : jre1.8.0_45 I finished debugging my Elliptic Curve class and in logging all characteristics of the keys I also log the BIT length of the keys (which with Elliptic Curve is...
Jon Skeet
people
quotationmark

It's working as intended and as documented. From the documentation: Returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit. For positive BigIntegers, this is equivalent to... more 8/10/2015 11:36:25 AM

people

Where does a generic List<> implement Reset?

when I go to the definition of List< I can see it has a public struct Enumerator that implements the interfaces IEnumerator<T>, IDisposable and IEnumerator. ...
Jon Skeet
people
quotationmark

It's explicitly implemented, as shown in the documentation, as is IEnumerator.Current. In other words, you can only call the method on a value with a compile-time type of IEnumerator. So you could use: // Casing changed to be more... more 8/10/2015 8:49:41 AM

people

What does {} mean in C#'

I have a DataGridView cell that is of type System.Single. if (myDataGridView.Rows[0].Cells[1].Value == null) { //some code } myDataGridView.Rows[0].Cells[1].Value has...
Jon Skeet
people
quotationmark

As noted, the value is an instance of DbNull. Given that DbNull.Value is a singleton (and thus safe for reference comparisons), two options come to mind: if (myDataGridView.Rows[0].Cells[1].Value == DBNull.Value) or if... more 8/10/2015 6:17:49 AM

people

How can i send a byte daha which is greater than 127 in Java?

I am developing an RFID reader application using Java. The RFID reader's message type has to start with 0xA0. It is obligatory. It is represented as 160 in decimal form. But in...
Jon Skeet
people
quotationmark

Just send (byte) 0xa0. Yes, as a Java byte it will be negative, but it will still have the right bit pattern, which is all the RFID will care about. Basically, you rarely need to think of bytes as numbers as such - it's more usual to... more 8/9/2015 7:07:27 PM

people

Java issue compiling code in command line: Package does not exist...but it does

I'm trying to compile my Java code with this command (OS X Yosemite): javac -cp "../../;./colorCalculator;" Application.java but I get the following errors (among others that...
Jon Skeet
people
quotationmark

The classpath is meant to include the root of the package structure for any appropriate directory. So the compiler is currently looking for ../../colorCalculator/Model.class or ./colorCalculator/colorCalculator/Model.class when you... more 8/9/2015 5:53:47 PM

people