Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Cannot access HasValue and Value Property of Nullable class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Aamir Mahmood - 22 Feb 2008 01:17 GMT
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
Peter Duniho - 22 Feb 2008 01:43 GMT
> Hi
>
[quoted text clipped - 8 lines]
> It does not compile.  VS2005 says "System.Nullable does not contain a
> definition of HasValue".

And, if you look at the Nullable class, it doesn't.  Nullable<int>, a  
struct, does, but Nullable doesn't.

> 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.  
"creating an instance of the correct Nullable<T> type" based on the  
underlying type you get from GetUnderylingType()).

> 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
Michael Starberg - 22 Feb 2008 02:10 GMT
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
Aamir Mahmood - 22 Feb 2008 07:01 GMT
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.

> I'd like to see you type on a keyboard. It must be of iron and go red hot.
>
[quoted text clipped - 3 lines]
>
> - Michael Starberg
Peter Duniho - 22 Feb 2008 07:43 GMT
> 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  
use reflection, you'd need an instance of the Nullable<T> class itself,  
but that's not what you get when you assign a nullable value to object.

> 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  
other class from which they derive.  And if you look at the Nullable<T>  
docs, you'll note that it doesn't implement any other interface.

> 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
Marc Gravell - 22 Feb 2008 07:53 GMT
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
Michael Starberg - 22 Feb 2008 02:04 GMT
> Hi
>
[quoted text clipped - 7 lines]
> }
> 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
Jon Skeet [C# MVP] - 22 Feb 2008 07:50 GMT
> I am unable to access the HasValue and Value properties in an Nullable
> object.
[quoted text clipped - 8 lines]
> 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>.

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

Marc Gravell - 22 Feb 2008 07:59 GMT
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
jehugaleahsa@gmail.com - 23 Feb 2008 04:17 GMT
> Hi
>
[quoted text clipped - 30 lines]
>
> 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. ???

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.