Re: Checking for DBNull with generics

"Marc Gravell" <marc.gravell [at] gmail.com> wrote in message
news:uz5%23tjxkIHA.3888 [at] 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<bool> etc is doing; it
> *looks* like it is converting an empty Nullable<bool> to object (which
> should yield null due to the boxing rules), then converting that null to a
> T, which we already know if Nullable<bool> (hence becoming another empty
> Nullable<bool>)- 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<T>(object pReaderVar)
{
if (pReaderVar.Equals(DBNull.Value))
{
if (typeof(T) == typeof(string))
{
return (T)(object)"";
}
else if (typeof(T) == typeof(Nullable<bool>))
{
return (T)(object)new Nullable<bool>();
}
else
{
return default(T);
}
}
else
{
return (T)pReaderVar;
}
}

and yes, it returns null for a Nullable<bool>. I wonder why it's different
for VB.NET? Would be interesting to look at the MSIL.
Leon Mayne [ Mo, 31 März 2008 14:44 ] [ ID #1931360 ]

Re: Checking for DBNull with generics

"Leon Mayne" <leon [at] rmvme.mvps.org> wrote in message
news:C5A348E0-11EE-48E0-B45B-FAB3BDFA0295 [at] 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<bool>
IL_00be: call !!0
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerService s.Conversions::ToGenericParameter<!!0>(object)

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

So the Conversions::ToGenericParameter works but the direct unboxing
doesn't.
Leon Mayne [ Mo, 31 März 2008 15:01 ] [ ID #1931362 ]

Re: Checking for DBNull with generics

"Leon Mayne" <leon [at] rmvme.mvps.org> wrote in message
news:B27662E5-EC82-4A95-B97A-A023D69262E0 [at] microsoft.com...
>
> "Leon Mayne" <leon [at] rmvme.mvps.org> wrote in message
> news:C5A348E0-11EE-48E0-B45B-FAB3BDFA0295 [at] 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<bool>
> IL_00be: call !!0
> [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerService s.Conversions::ToGenericParameter<!!0>(object)
>
> C#:
> IL_0066: box valuetype [mscorlib]System.Nullable`1<bool>
> 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<T>(new
Nullable<Boolean>());

Something odd going on here!
Leon Mayne [ Mo, 31 März 2008 16:55 ] [ ID #1931374 ]
Microsoft » microsoft.public.dotnet.general » Re: Checking for DBNull with generics

Vorheriges Thema: Re: install old visual studio .net version
Nächstes Thema: new process should be asynchronous