Nullable object must have a value

Discussion in 'ASP .Net' started by =?Utf-8?B?emlubw==?=, Apr 13, 2007.

  1. in .net 2.0, the class (myClass) contains a property defined as:
    private _creationDate as As Nullable(Of DateTime)
    Public Property CreationDate() As Nullable(Of DateTime)
    Get
    If _creationDate.HasValue Then
    Return _creationDate
    Else
    Return Nothing
    End If
    End Get
    Set(ByVal value As Nullable(Of DateTime))
    If value.HasValue Then _creationDate= value
    End Set
    End Property


    when I call this property on the aspx page to assign its value to a text
    field as:
    <input type=text id=myText ... .. ..>
    .....
    .... .. ..
    myText.value = myCylass.CreationDate

    it return "Nullable object must have a value", unless I test for the NULL
    value as:
    if CreationDate.HasValue then myText.value= CreationDate (will work)

    I don't understand why the property cannot return NULL when its return type
    is set as (Nullable of DateTime).
    =?Utf-8?B?emlubw==?=, Apr 13, 2007
    #1
    1. Advertising

  2. It is not the nullable value that is the problem, but rather trying to set a
    NULL to a string value, which is not nullable. This means the textbox is
    puking,not your value.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA
    http://gregorybeamer.spaces.live.com

    *********************************************
    Think outside the box!
    *********************************************
    "zino" <> wrote in message
    news:...
    > in .net 2.0, the class (myClass) contains a property defined as:
    > private _creationDate as As Nullable(Of DateTime)
    > Public Property CreationDate() As Nullable(Of DateTime)
    > Get
    > If _creationDate.HasValue Then
    > Return _creationDate
    > Else
    > Return Nothing
    > End If
    > End Get
    > Set(ByVal value As Nullable(Of DateTime))
    > If value.HasValue Then _creationDate= value
    > End Set
    > End Property
    >
    >
    > when I call this property on the aspx page to assign its value to a text
    > field as:
    > <input type=text id=myText ... .. ..>
    > ....
    > ... .. ..
    > myText.value = myCylass.CreationDate
    >
    > it return "Nullable object must have a value", unless I test for the NULL
    > value as:
    > if CreationDate.HasValue then myText.value= CreationDate (will work)
    >
    > I don't understand why the property cannot return NULL when its return
    > type
    > is set as (Nullable of DateTime).
    >
    >
    >
    >
    >
    Cowboy \(Gregory A. Beamer\), Apr 13, 2007
    #2
    1. Advertising

  3. that's what I thought in the begining, and I tried the following and it worked:
    myText.Value = Nothing


    could it be the "Nothing" returned by the property is coneverted to
    something I don't understand ?
    =?Utf-8?B?emlubw==?=, Apr 13, 2007
    #3
  4. =?Utf-8?B?emlubw==?=

    carion1 Guest

    Nothing != Null

    You made the property nullable but as Greg stated above a textbox can
    not hold a null value. This seems dirty to me but the following
    works:

    foobar.Text = yourClass.CreationDate.ToString()

    On Apr 13, 2:56 pm, zino <> wrote:
    > that's what I thought in the begining, and I tried the following and it worked:
    > myText.Value = Nothing
    >
    > could it be the "Nothing" returned by the property is coneverted to
    > something I don't understand ?
    carion1, Apr 13, 2007
    #4
  5. Try setting up a null string object and binding to the value and watch what
    happens, or better yet, an object that has string values, which can accept
    null.

    The VB.NET compiler takes some shortcuts, when it can, to protect you from
    problems. While Nothing (VB) and null (C#) are functionally equivalent, the
    compilers are different and make different assumptions.

    While I have not tried

    ..Value = Nothing

    I would imagine the compiler substitutes an empty string
    (System.String.Empty) for Nothing in that case.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA
    http://gregorybeamer.spaces.live.com

    *********************************************
    Think outside the box!
    *********************************************
    "zino" <> wrote in message
    news:...
    > that's what I thought in the begining, and I tried the following and it
    > worked:
    > myText.Value = Nothing
    >
    >
    > could it be the "Nothing" returned by the property is coneverted to
    > something I don't understand ?
    >
    Cowboy \(Gregory A. Beamer\), Apr 13, 2007
    #5
  6. Hi Zino,

    As other members has said, the problem here is that for Nullable instance,
    it require you to assign it a value before use it to populate other data
    field. Or if you want to assign a nullable instance to your textbox.Text
    property without explicitly detect whether it "hasvalue", you can use the
    "GetValueOrDefault" method to return the value or default value(if it is in
    null state). e.g.


    ================
    txtNullable.Text = Me.CreationDate.GetValueOrDefault()
    =================

    Here is the msdn reference which has documented those characterisitcs of
    Nullable type object:

    #Nullable Types (C# Programming Guide)
    http://msdn2.microsoft.com/en-us/library/1t3y8s4s.aspx

    Hope this also helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Apr 16, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QURhdmlkc29u?=
    Replies:
    3
    Views:
    2,280
    =?Utf-8?B?QURhdmlkc29u?=
    Mar 4, 2005
  2. Nikola Skoric
    Replies:
    4
    Views:
    1,982
    Colin Marquardt
    Jun 15, 2006
  3. ina
    Replies:
    4
    Views:
    525
    Andy Dingley
    Jun 13, 2006
  4. Nemisis

    Nullable object types

    Nemisis, Aug 18, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    6,493
    Patrice
    Aug 21, 2006
  5. scparker

    Nullable object must have a value.

    scparker, Jan 16, 2007, in forum: ASP .Net
    Replies:
    15
    Views:
    1,965
    scparker
    Jan 18, 2007
Loading...

Share This Page