> I have the following in c# VS 2005.
>
[quoted text clipped - 17 lines]
>
> but that didn't work either.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
>> I have the following in c# VS 2005.
>>
[quoted text clipped - 23 lines]
> Find out what dbReader["ProjectTableID"].GetType() shows. I suspect it
> won't be System.Int32.
It isn't.
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.
Thanks,
Tom
Arne Vajhøj - 23 Feb 2008 01:07 GMT
> 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.
[quoted text clipped - 8 lines]
>
> {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.
Arne
tshad - 24 Feb 2008 06:58 GMT
>> 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.
[quoted text clipped - 15 lines]
>
> 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
Jon Skeet [C# MVP] - 24 Feb 2008 07:23 GMT
<snip>
> That works fine.
>
[quoted text clipped - 7 lines]
>
> I would have assumed that int would also work but I guess not.
No, because you're unboxing. When you unbox, you have to specify the
correct type, not just one which the correct type can be implicitly
converted to.
(You can also specify the nullable version of the correct type.)

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Arne Vajhøj - 24 Feb 2008 16:57 GMT
>>> 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.
[quoted text clipped - 25 lines]
>
> I would have assumed that int would also work but I guess not.
Maybe you will find this code snippet illustrative:
using System;
namespace E
{
public class Program
{
public static void Main(string[] args)
{
byte b = 123;
object o = b;
Console.WriteLine("Cast to byte works: " + (byte)o);
try{
Console.WriteLine("Cast to int works: " + (int)o);
}
catch
{
Console.WriteLine("Cast to int did not work");
}
Console.WriteLine("Cast to byte cast to int works: " + (int)(byte)o);
Console.ReadKey();
}
}
}
Arne
tshad - 24 Feb 2008 18:35 GMT
>>>> It depends. The type in SQL is tinyint. In my record
>>>> fn.ProjectTableID is an "int?". The value will either be null or a
[quoted text clipped - 53 lines]
> }
> }
(int)(byte)o
So what you did here was unbox the object and then implicitly convert it to
int?
And why is dbReader["ProcessingType"] considered boxed?
Thanks,
Tom
> Arne
Jon Skeet [C# MVP] - 24 Feb 2008 19:07 GMT
<snip>
> (int)(byte)o
>
> So what you did here was unbox the object and then implicitly convert it to
> int?
Well, explicitly convert it to int - but yes.
> And why is dbReader["ProcessingType"] considered boxed?
Because its return type is "object" - i.e. it will return a reference,
so the only way of returning value type data is to return a boxed form.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jon Skeet [C# MVP] - 23 Feb 2008 07:24 GMT
<snip>
> It isn't.
>
[quoted text clipped - 10 lines]
>
> {Name = "Byte" FullName = "System.Byte"} System.Type {System.RuntimeType}
Right - that's the important part.
> I would assume that I would be casting it to either int? or int since I am
> putting it in an int? field.
The important thing is what you're casting. You're trying to unbox,
which means the type has to be right. You can cast it to byte? (or
indeed byte) and it'll be fine. Cast the null to int? so that compiler
knows what to make the overall expression type.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk