.NET Framework - Nullable object must have a value

Asked By zin on 13-Apr-07 03:40 PM
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:
....
... .. ..
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\) replied on 13-Apr-07 03:48 PM
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!
*********************************************
zin replied on 13-Apr-07 03:56 PM
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 replied on 13-Apr-07 04:41 PM
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()
Cowboy \(Gregory A. Beamer\) replied on 13-Apr-07 05:01 PM
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!
*********************************************
stchen replied on 15-Apr-07 11:42 PM
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
null state). e.g.


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

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

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.