0

In code like so:

DataSet dsDuckbill = new DataSet();
DataTable dtDuckbill = dsDuckbill.Tables.Add("DSD");
dtDuckbill.Columns.Add("line_id", Type.GetType("System.Int32"));
dtDuckbill.Columns["line_id"].AutoIncrement = true;
dtDuckbill.Columns["line_id"].AutoIncrementSeed = 1;
dtDuckbill.Columns["line_id"].AutoIncrementStep = 1;

dtDuckbill.Columns.Add("vendor_id", Type.GetType("System.String"));
. . .

I get several, "Possible 'null' assignment to entity marked with 'NotNull' attribute"

I'm not sure if this is the right way to do it, but I was able to get past the first one this way:

DataSet dsDuckbill = new DataSet();
if (dsDuckbill.Tables.Contains("DSD"))
{
    DataTable dtDuckbill = dsDuckbill.Tables.Add("DSD");

...but I'm still getting that err msg on the first line for each line with "Type" (Resharper highlights "Type" as the culprit).

What's up with that (System.Int32 and System.String are possibly null?), and what need I do to mollify the R# beast?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • Why don't you do `typeof(int)` and `typeof(string)`? – Mike Zboray Sep 23 '13 at 16:24
  • Some of the types are Int16 and some are Int32; there are also doubles as well as the strings. I fear changing all the ints to a simple int in case the 16s and 32s are dealt with differently. IOW, I don't want to clean up the code so much that I break it somehow. – B. Clay Shannon-B. Crow Raven Sep 23 '13 at 16:38
  • 2
    I think it flags those because it can't be sure that the string you pass into the GetType method is valid. What if you mistyped the argument as `System.Int33` (typo). The result would be null and that is what you are being warned about, I believe. – Chris Dunaway Sep 23 '13 at 17:19
  • 3
    @ClayShannon then use `typeof(Int16)` and `typeof(Int32)`. I see no reason to use hard-coded strings instead of the `typeof` operator here. `GetType` returns null if the type is not found and obviously R# isn't executing the expression just looking at the return type. – Mike Zboray Sep 23 '13 at 18:42

0 Answers0