Re: Checking for DBNull with generics

Re: Checking for DBNull with generics

am 31.03.2008 14:44:40 von Leon Mayne

"Marc Gravell" wrote in message
news:uz5%23tjxkIHA.3888@TK2MSFTNGP03.phx.gbl...
> Just for the record, string-compare on GetType is a nasty way to do
> things - surely comparing just the types [i.e. GetType(T) vs
> GetType(String) etc] would be cleaner? Granted: you can't do this in a
> switch... but If, ElseIf, etc should do.

Yes, that's why I was doing a string compare. Perhaps I should rewrite it as
you suggest for performance.

> I'm also a little confused as to what the Nullable etc is doing; it
> *looks* like it is converting an empty Nullable to object (which
> should yield null due to the boxing rules), then converting that null to a
> T, which we already know if Nullable (hence becoming another empty
> Nullable)- so we've got a complicated way of saying "null". In C#,
> default(T) here would the same thing... I don't know what VB does, though.

I just checked this and it seems to work fine. It creates a new nullable(of
boolean) with no value, and HasValue = false. Is this different in C#?
*Leon checks
OK, I just tried:

public static T CheckDBNull(object pReaderVar)
{
if (pReaderVar.Equals(DBNull.Value))
{
if (typeof(T) == typeof(string))
{
return (T)(object)"";
}
else if (typeof(T) == typeof(Nullable))
{
return (T)(object)new Nullable();
}
else
{
return default(T);
}
}
else
{
return (T)pReaderVar;
}
}

and yes, it returns null for a Nullable. I wonder why it's different
for VB.NET? Would be interesting to look at the MSIL.

Re: Checking for DBNull with generics

am 31.03.2008 15:01:37 von Leon Mayne

"Leon Mayne" wrote in message
news:C5A348E0-11EE-48E0-B45B-FAB3BDFA0295@microsoft.com...
> Would be interesting to look at the MSIL.

I just did this, and it looks like the VB version is using a different call
to unbox the object:

VB:
IL_00b9: box valuetype [mscorlib]System.Nullable`1
IL_00be: call !!0
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerService s.Conversions::ToGenericParameter(object)

C#:
IL_0066: box valuetype [mscorlib]System.Nullable`1
IL_006b: unbox.any !!T

So the Conversions::ToGenericParameter works but the direct unboxing
doesn't.

Re: Checking for DBNull with generics

am 31.03.2008 16:55:14 von Leon Mayne

"Leon Mayne" wrote in message
news:B27662E5-EC82-4A95-B97A-A023D69262E0@microsoft.com...
>
> "Leon Mayne" wrote in message
> news:C5A348E0-11EE-48E0-B45B-FAB3BDFA0295@microsoft.com...
>> Would be interesting to look at the MSIL.
>
> I just did this, and it looks like the VB version is using a different
> call to unbox the object:
>
> VB:
> IL_00b9: box valuetype [mscorlib]System.Nullable`1
> IL_00be: call !!0
> [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerService s.Conversions::ToGenericParameter(object)
>
> C#:
> IL_0066: box valuetype [mscorlib]System.Nullable`1
> IL_006b: unbox.any !!T
>
> So the Conversions::ToGenericParameter works but the direct unboxing
> doesn't.

Just tried it myself:

Return
Microsoft.VisualBasic.CompilerServices.Conversions.ToGeneric Parameter(Of
T)(New Nullable(Of Boolean))

Works fine in VB.NET, but not in C#:

return
Microsoft.VisualBasic.CompilerServices.Conversions.ToGeneric Parameter(new
Nullable());

Something odd going on here!