473,394 Members | 1,717 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,394 software developers and data experts.

Nullable(Of Single) VB.NET - Setting = 0 results in Nothing

I am using VB.NET and I have a variable that is defined as Nullable(Of
Single) as follows:

Dim fMyNullableSingle as Nullable(Of Single)

Then, I have another variable as follows:

Dim fCumulativeExpectedAmt As Single = 0

When I make the following assignment:

fMyNullableSingle = fCumulativeExpectedAmt

the end result is that fMyNullableSingle is Nothing. So, later, when I
try to assign it to another non-nullable variable, it fails.For
example...

Dim fMyNonNullableSingle as Single
fMyNonNullableSingle = fMyNullableSingle

causes an Exception.

How do I correct this?
Oct 6 '08 #1
2 3329
I am using VB.NET and I have a variable that is defined as Nullable(Of
Single) as follows:

* Dim fMyNullableSingle as Nullable(Of Single)

Then, I have another variable as follows:

* Dim fCumulativeExpectedAmt As Single = 0

When I make the following assignment:

* fMyNullableSingle = fCumulativeExpectedAmt

the end result is that fMyNullableSingle is Nothing.
I don't get that, when I assign zero to the Nullable(Of Single) it
returns zero:

////////////
Module Module1

Sub Main()
Dim s As Nullable(Of Single)
Dim s2 As Single = 0

Console.WriteLine(s.HasValue)
If s.HasValue Then Console.WriteLine(s.Value)

s = s2

Console.WriteLine(s.HasValue)
If s.HasValue Then Console.WriteLine(s.Value)

Console.Read()
End Sub

End Module
////////////
So, later, when I
try to assign it to another non-nullable variable, it fails.For
example...

* Dim fMyNonNullableSingle as Single
* fMyNonNullableSingle = fMyNullableSingle

causes an Exception.

How do I correct this?
It should cause an exception - that's an invalid cast. Simply do the
following to correct the problem:

//////////////
fMyNonNullableSingle = fMyNullableSingle.Value
//////////////

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Oct 6 '08 #2
I don't get that, when I assign zero to the Nullable(Of Single) it
returns zero:
I erred in not mentioning that the setting was happening in a Property
Set, and therein was the problem. The code that I had there was
checking, incorrectly, to see if the new value was different from the
existing value, and only setting the property if it was actually
different. Well, the code to check for difference did not use
the .Equals method of the Nullable type, and so was never setting the
property equal to ZERO! My mistake...
* Dim fMyNonNullableSingle as Single
* fMyNonNullableSingle = fMyNullableSingle
causes an Exception.
How do I correct this?

It should cause an exception - that's an invalid cast. Simply do the
following to correct the problem:

//////////////
fMyNonNullableSingle = fMyNullableSingle.Value
//////////////
I have equated variables like this many times, without the
explicit .Value at the end of the Nullable one, with no known
problems. Perhaps it's just more "proper" to use the .Value?
Oct 7 '08 #3

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

Similar topics

0
by: Larry Lard | last post by:
There seems to be something a bit lacking in the way the dataset designer thing deals (or rather doesn't) with nullable fields in VS2005. Maybe it's cos I'm using VB2005 Express (which is variously...
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: MobileBoy36 | last post by:
Hi all, Nullable types were announced as new handy stuff in .NET 2.0 But it seems like the datareader doesn't support nullable types. You have still to check for "IsDbNull". So, are Nullable...
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...
15
by: scparker | last post by:
I have yet to find a satisfactory solution to this problem. It involves VB.NET 2.0 and datetime issues. I have a form that asks for a Date to be submitted in dd/mm/yyyy format. When this is...
5
by: =?Utf-8?B?emlubw==?= | last post by:
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...
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...
6
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have a generic function I am using to check constraints, an example of which is the following: Public Function ValidateMinValue(Of t As IComparable)(ByVal PropertyName As String, ByVal value...
0
by: Rick | last post by:
VS2005 Pro I'm binding to a custom business object with type Nullable(of date) and trying to validate user input. I have an error provider connected to the binding object which is connected...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
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...

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.