How to tell difference for 0.0 and Nothing for variable in vb.net 2005
-
Hi, dear all, I create a variable as following Dim xx as double at now, it will return true if I check as following since I didn't initialize it. if xx = Nothing then ... end if 'RETURN TRUE But the problem is that after I initialize it to cdbl(0), it still return true while check Nothing. xx = cdbl(0) if xx = Nothing then ...end if ''''STILL RETURN TRUE so how can I tell the difference for a variable initialized or not? Thanks!
-
Hi, dear all, I create a variable as following Dim xx as double at now, it will return true if I check as following since I didn't initialize it. if xx = Nothing then ... end if 'RETURN TRUE But the problem is that after I initialize it to cdbl(0), it still return true while check Nothing. xx = cdbl(0) if xx = Nothing then ...end if ''''STILL RETURN TRUE so how can I tell the difference for a variable initialized or not? Thanks!
You can't. Not for a simple type. They're automatically initialized to zero as soon as you create them, so for a double,
Cbl(0)
andNothing
are the same thing. If you're looking for an error code, or a way to ensure that it receives a value, you could just initialize it todouble.NaN
(NaN = Not a Number, the same result you get when dividing by zero). Then just test whetherdouble.IsNaN(xx)
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
You can't. Not for a simple type. They're automatically initialized to zero as soon as you create them, so for a double,
Cbl(0)
andNothing
are the same thing. If you're looking for an error code, or a way to ensure that it receives a value, you could just initialize it todouble.NaN
(NaN = Not a Number, the same result you get when dividing by zero). Then just test whetherdouble.IsNaN(xx)
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)Ian, Thank you very much, I will try it.
-
Hi, dear all, I create a variable as following Dim xx as double at now, it will return true if I check as following since I didn't initialize it. if xx = Nothing then ... end if 'RETURN TRUE But the problem is that after I initialize it to cdbl(0), it still return true while check Nothing. xx = cdbl(0) if xx = Nothing then ...end if ''''STILL RETURN TRUE so how can I tell the difference for a variable initialized or not? Thanks!
This is what nullable value types are commonly used for: Dim xx As Double? or: Dim xx As Nullable(Of Double) and then check if it's been assigned: Dim isAssigned As Boolean = xx.HasValue
David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com
-
This is what nullable value types are commonly used for: Dim xx As Double? or: Dim xx As Nullable(Of Double) and then check if it's been assigned: Dim isAssigned As Boolean = xx.HasValue
David Anton Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com
Thanks, David, it's very useful.