Re: Nullable types giving error

Tech-Archive recommends: Fix windows errors by optimizing your registry




"Arne Vajhøj" <arne@xxxxxxxxxx> wrote in message
news:47bf71e2$0$90263$14726298@xxxxxxxxxxxxxxxxxx
tshad wrote:
It depends. The type in SQL is tinyint. In my record fn.ProjectTableID
is an "int?". The value will either be null or a number from 1-25.

If it is null, which is the case for the first 12 records, I get the
following in the debugger watch window

{Name = "DBNull" FullName = "System.DBNull"} System.Type
{System.RuntimeType}

:If it is a value like 10, I get:

{Name = "Byte" FullName = "System.Byte"} System.Type {System.RuntimeType}
^^^^^^^^^^^

I would assume that I would be casting it to either int? or int since I
am putting it in an int? field. I can't change the fn.ProjectTableID to
int (or byte) as it can be null and I need to keep track of that.

Try cast it to byte.

That works fine.

Since a tinyint ranges from 0 to 255, I assume that is why it is assumed to
be a byte.

fn.ProcessingType = dbReader["ProcessingType"] == DBNull.Value ? null :
(byte?)dbReader["ProcessingType"];

This works ok

I would have assumed that int would also work but I guess not.

Thanks,

Tom

Arne


.



Relevant Pages

  • Re: Newbie question about malloc
    ... "Implicit int" no longer exists in C. ... It's generally recommended to NOT cast the return from malloc. ... %d can never be the correct format specifier for a size_t. ... C99 introduced %zu for this purpose, ...
    (comp.lang.c)
  • Re: what will happen after i use free()???
    ... Inserting a cast before malloc is not needed, ... 56 bits while int is only 48 bits. ... So casting that value requires some ... Use cast only if you are absolutely sure that yor really needs the ...
    (comp.lang.c)
  • Re: about the array
    ... 3- If you cast to the wrong type by accident, ... are malloc and free. ... the compiler should issue a diagnostic - gcc ... unknown set of arguments, and return an int. ...
    (comp.lang.c)
  • Re: Simple Casting Question
    ... only in the form (int to float),,, ... or just casting identical types, so I hope that I can avoid some ... None of the conversions you describe ... In most cases where a cast is used, ...
    (comp.lang.c)
  • swap using pointers
    ... int swap; ... incompatible pointer type ...
    (comp.lang.c)