473,657 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.H asValue 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.Creati onDate

it return "Nullable object must have a value", unless I test for the NULL
value as:
if CreationDate.Ha sValue 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 2946
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.n oemailwrote in message
news:22******** *************** ***********@mic rosoft.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.H asValue 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.Creati onDate

it return "Nullable object must have a value", unless I test for the NULL
value as:
if CreationDate.Ha sValue 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.Creat ionDate.ToStrin g()

On Apr 13, 2:56 pm, zino <z...@noemail.n oemailwrote:
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.n oemailwrote in message
news:5D******** *************** ***********@mic rosoft.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
"GetValueOrDefa ult" method to return the value or default value(if it is in
null state). e.g.
=============== =
txtNullable.Tex t = Me.CreationDate .GetValueOrDefa ult()
=============== ==

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
5956
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 specific type the nullable type is.) The reason I'm trying this is because when I attempt to pass a nullable type value to a SqlCommand parameter and then attempt to execute it I the following error: "No mapping exists from object type...
30
4567
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 corresponding field in the lookup table. In my data table we store the ID in what I will call the 'key' field. == Description of the desired operation:
6
3827
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 database for the given ID. I could just leave two separate constructors, or i could just have the default one call the second with a "-1" as the ID. This means that the initialization of certain fields can be based on whether my ID is -1 (for lazy...
3
2100
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 that I got when I attempted to perform some simple tests. Nullable<int> i = 32; bool isNullableClass = i is Nullable; // false
1
5676
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) http://msdn2.microsoft.com/en-us/library/ms366789.aspx however, using their code snippet, I couldn't get it to work:
3
2578
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 nullable (Of integer) Public Property ID() Get
3
2016
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 adapters ; you know those clever little things which allow you to contrsuct methods for manipulating data use this. So a method might be declared someting like this. Private Sub myMethod(ByVal myYear As System.Nullable(Of Integer), ByVal...
2
3800
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 get set(ByVal Value as Nullable(Of Int16)) If not _MyProp.Equals(value) then
4
6414
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
8399
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.