Nullable object must have a value

G

Guest

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).
 
C

Cowboy \(Gregory A. Beamer\)

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.
 
G

Guest

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 ?
 
C

carion1

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()
 
C

Cowboy \(Gregory A. Beamer\)

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.
 
S

Steven Cheng[MSFT]

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.
 

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

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top