Home All Groups Group Topic Archive Search About

How to get value of nullable of unknown type??

Author
11 Oct 2007 5:35 AM
hvj
Hi,

I have a situation that I have a nullable type that can be a int?,
double? or any other nullable type. How can I get the contained value
of the nullable without knowing its type.

I read that in the .NET2.0 beta2 there used to be a INullable
interface that could be used for this, but that interface was
skipped.

Thanks,
Henk

Author
11 Oct 2007 12:10 PM
Jon Skeet [C# MVP]
On Oct 11, 6:35 am, h***@paralax.nl wrote:
> I have a situation that I have a nullable type that can be a int?,
> double? or any other nullable type. How can I get the contained value
> of the nullable without knowing its type.

Just box it, and the result will either be the boxed value, or null.

It's not clear to me what sort of interface you'd be looking to use
for this anyway.

Jon
Author
15 Oct 2007 1:41 PM
hvj
On Oct 11, 2:10 pm, "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
> On Oct 11, 6:35 am, h***@paralax.nl wrote:
>
> > I have a situation that I have a nullable type that can be a int?,
> > double? or any other nullable type. How can I get the contained value
> > of the nullable without knowing its type.
>
> Just box it, and the result will either be the boxed value, or null.
>
> It's not clear to me what sort of interface you'd be looking to use
> for this anyway.
>
> Jon

Thanks,

Your sollution transforms the problem to 'how can I get the content of
a boxed value without knowing the boxed type', which is as far as I
know
1 checking the type
2 assigning to a reference of that type.
Author
15 Oct 2007 4:54 PM
Jon Skeet [C# MVP]
<h**@paralax.nl> wrote:
Show quote
> On Oct 11, 2:10 pm, "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
> > On Oct 11, 6:35 am, h***@paralax.nl wrote:
> >
> > > I have a situation that I have a nullable type that can be a int?,
> > > double? or any other nullable type. How can I get the contained value
> > > of the nullable without knowing its type.
> >
> > Just box it, and the result will either be the boxed value, or null.
> >
> > It's not clear to me what sort of interface you'd be looking to use
> > for this anyway.
>
> Your sollution transforms the problem to 'how can I get the content of
> a boxed value without knowing the boxed type', which is as far as I
> know
> 1 checking the type
> 2 assigning to a reference of that type.

Well how would you possibly represent the value in the first place?
What kind of variable could hold it? Surely it's just "object", in
which case the boxed value is exactly the right answer in the first
place.

It would be very helpful if you could post some code which would try to
use this, so we could get more of an idea of your requirements.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
16 Oct 2007 8:31 AM
hvj
On Oct 15, 6:54 pm, Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Show quote
>  <h***@paralax.nl> wrote:
> > On Oct 11, 2:10 pm, "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote:
> > > On Oct 11, 6:35 am, h***@paralax.nl wrote:
>
> > > > I have a situation that I have a nullable type that can be a int?,
> > > > double? or any other nullable type. How can I get the contained value
> > > > of the nullable without knowing its type.
>
> > > Just box it, and the result will either be the boxed value, or null.
>
> > > It's not clear to me what sort of interface you'd be looking to use
> > > for this anyway.
>
> > Your sollution transforms the problem to 'how can I get the content of
> > a boxed value without knowing the boxed type', which is as far as I
> > know
> > 1 checking the type
> > 2 assigning to a reference of that type.
>
> Well how would you possibly represent the value in the first place?
> What kind of variable could hold it? Surely it's just "object", in
> which case the boxed value is exactly the right answer in the first
> place.
>
> It would be very helpful if you could post some code which would try to
> use this, so we could get more of an idea of your requirements.
>
> --
> Jon Skeet - <sk***@pobox.comhttp://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too- Hide quoted text -
>
> - Show quoted text -

Thanks,

You've been very helpfull allready. The boxing does it.

Regards,
Henk

AddThis Social Bookmark Button