Cannot access HasValue and Value Property of Nullable class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aamir Mahmood

    Cannot access HasValue and Value Property of Nullable class

    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).HasVa lue) {
    ...
    }
    It does not compile. VS2005 says "System.Nullabl e 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


  • Michael Starberg

    #2
    Re: Cannot access HasValue and Value Property of Nullable class


    "Aamir Mahmood" <aamirmahmood@g mail.comwrote in message
    news:eWqAFDPdIH A.1376@TK2MSFTN GP02.phx.gbl...
    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).HasVa lue) {
    ...
    }
    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




    Comment

    • Michael Starberg

      #3
      Re: Cannot access HasValue and Value Property of Nullable class


      "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
      news:op.t6wkezg t8jd0ej@petes-computer.local. ..

      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



      Comment

      • Marc Gravell

        #4
        Re: Cannot access HasValue and Value Property of Nullable class

        Nullable<Thas special boxing rules that return an empty Nullable<Tas 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


        Comment

        • Marc Gravell

          #5
          Re: Cannot access HasValue and Value Property of Nullable class

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

          public void DoSomething<T>( Nullable<Tpoten tialValue) 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


          Comment

          Working...