Cannot access HasValue and Value Property of Nullable class

A

Aamir Mahmood

Hi

I am unable to access the HasValue and Value properties in an Nullable
object.

int? i = 9;
object value = i;
if ((value as Nullable).HasValue) {
...
}
It does not compile. VS2005 says "System.Nullable does not contain a
definition of HasValue".
I want to achieve this to make just one function which can handle various
nullables like int?, decimal?, long? etc.


However, following code compile and runs OK for me.

int? i = 9;
if (i.HasValue) {
...
}

MSDN Says:
Fundamental Properties:
The two fundamental members of the Nullable structure are the HasValue and
Value properties. If the HasValue property for a Nullable object is true,
the value of the object can be accessed with the Value property.

Any ideas?

Thanks.

AM
 
P

Peter Duniho

Hi

I am unable to access the HasValue and Value properties in an Nullable
object.

int? i = 9;
object value = i;
if ((value as Nullable).HasValue) {
...
}
It does not compile. VS2005 says "System.Nullable does not contain a
definition of HasValue".

a said:
I want to achieve this to make just one function which can handle various
nullables like int?, decimal?, long? etc.

I'm not an expert on nullable types, but I think this may be non-trivial..
The Nullable<T> type can give you the HasValue property, but you need to
somehow get that structure, and doing so at run-time doesn't look
straightforward to me. The most obvious route I see uses reflection,
starting with the Nullable.GetUnderylingType() method, somehow creating an
instance of the correct Nullable<T> type, and using that to get the value
of the HasValue property (again, via reflection seems most direct).

Hopefully I'm wrong, and if so I hope someone will correct me. Especially
since I'm not really clear on how that middle part would work (i.e.
However, following code compile and runs OK for me.

int? i = 9;
if (i.HasValue) {
...
}

Right, because there you're using Nullable<int>, not Nullable. You could
also just cast the object to int? and test the HasValue property, but
based on what you wrote that doesn't seem like that would solve your
problem.
MSDN Says:
Fundamental Properties:
The two fundamental members of the Nullable structure are the HasValue
and
Value properties. If the HasValue property for a Nullable object is true,
the value of the object can be accessed with the Value property.

Right. That's the docus for the generic Nullable<T> structure. But
that's not what you get when you use the Nullable class.

Pete
 
M

Michael Starberg

Aamir Mahmood said:
Hi

I am unable to access the HasValue and Value properties in an Nullable
object.

int? i = 9;
object value = i;
if ((value as Nullable).HasValue) {
...
}
It does not compile.

Of cource it won't.
You are way over your head..
... and have a lot of reading to do.

- Michael Starberg
 
M

Michael Starberg

I'd like to see you type on a keyboard. It must be of iron and go red hot.

Anyway, my reply was more effecient than yours, while we say pretty much the
same thing. =)
Mine might have been somewhat less helpful.. maybe...

- Michael Starberg
 
A

Aamir Mahmood

Ok, then what is the best way to do what I want to do?
Or is there any way at all (other than the reflection thing)?

If not Nullable class, is there any other abstract class or interface from
which all these structures derive the HasValue and Value members.

IMO, I have a very genuine reason to have something like this.
 
P

Peter Duniho

Ok, then what is the best way to do what I want to do?
Or is there any way at all (other than the reflection thing)?

Well, no one else has offered an alternative suggestion. I'd give it
another 12 hours or so to let some of the people in other timezones catch
up, but I'd say that it may be going through reflection is the only way.
But even there, it's not entirely clear on what you're trying to do. To
If not Nullable class, is there any other abstract class or interface
from
which all these structures derive the HasValue and Value members.

Nullable types are actually structs. So, no...obviously they have no
IMO, I have a very genuine reason to have something like this.

Why? In the code you posted, you could just check to see whether the
"value" variable is null or not.

What is it about the HasValue property specifically that you need it for?

Pete
 
J

Jon Skeet [C# MVP]

Aamir Mahmood said:
I am unable to access the HasValue and Value properties in an Nullable
object.

int? i = 9;
object value = i;
if ((value as Nullable).HasValue) {
...
}
It does not compile. VS2005 says "System.Nullable does not contain a
definition of HasValue".
I want to achieve this to make just one function which can handle various
nullables like int?, decimal?, long? etc.

Sure - it just needs to be a generic method:

public void DoSomething<T>(Nullable<T> potentialValue)
{
if (potentialValue.HasValue)
{
...
}
}

As others have pointed out, Nullable isn't the same as Nullable<T>.
 
M

Marc Gravell

Nullable<T> has special boxing rules that return an empty Nullable<T> as a
null reference, rather than a boxed empty sruct. This means that everything
you need is as simple as testing your object variable for null, and
casting - i.e.

if(value != null) {
// ...
}

Marc
 
M

Marc Gravell

Minor addition for the OP; you need a constraint for that generic method to
compile:

public void DoSomething<T>(Nullable<T> potentialValue) where T : struct
{...}

Of course, this won't help if *all* you know is that you have an "object
value", since the compiler won't allow this usage...

Marc
 
J

jehugaleahsa

Hi

I am unable to access the HasValue and Value properties in an Nullable
object.

int? i = 9;
object value = i;
if ((value as Nullable).HasValue) {
    ...}

It does not compile.  VS2005 says "System.Nullable does not contain a
definition of HasValue".
I want to achieve this to make just one function which can handle various
nullables like int?, decimal?, long? etc.

However, following code compile and runs OK for me.

int? i = 9;
if (i.HasValue) {
    ...

}

MSDN Says:
Fundamental Properties:
The two fundamental members of the Nullable structure are the HasValue and
Value properties. If the HasValue property for a Nullable object is true,
the value of the object can be accessed with the Value property.

Any ideas?

Thanks.

AM

Why don't you just cast it back into an int?

int? i = 9;
object o = i;
int? j = (int?)i;

Nullable is a class for finding information about a Nullable<T> type.
Nullable<T> is a struct that makes a value type, in a sense, null-
able.

I am not sure what the difficulty is here. ???
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top