473,385 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Nullable object must have a value

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

Apr 13 '07 #1
5 2928
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" <zi**@noemail.noemailwrote in message
news:22**********************************@microsof t.com...
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).


Apr 13 '07 #2
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 ?

Apr 13 '07 #3
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 <z...@noemail.noemailwrote:
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 ?

Apr 13 '07 #4
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" <zi**@noemail.noemailwrote in message
news:5D**********************************@microsof t.com...
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 ?
Apr 13 '07 #5
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.




Apr 16 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ESPNSTI | last post by:
Hi, Please don't shoot me if the answer is simple, I'm new to .Net and C# :) .. I'm attempting to convert a nullable type to it's "non-nullable" type in a generic way (without knowing what...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
6
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the...
3
by: rubikzube* | last post by:
Hi. I'm trying to write a snippet of code to detect if a variable is a nullable<t> struct, and I can't figure out the right syntax or if such a thing is even possible... Below are the results...
1
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide)...
3
by: Nemisis | last post by:
Hi everyone, i have been stressing over this for the past hour or so, and just cannot figure it out, sorry if i am being stupid, but it is a friday! lol I have the following Private _ID as...
3
by: Goofy | last post by:
The 2.0 Framework has a new type called System.Nullable. This allows something to Not exist, basically you can pass 'Nothing' to a method whose parameters as System.Nullable. Im looking at table...
2
by: Rick | last post by:
I have a business object (class) with some nullable properties declared like this: Private _MyProp as Nullable(of Int16) public Property MyProp as Nullable(Of Int16) get Return _MyProp end...
4
by: Aamir Mahmood | last post by:
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) { ... }
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.