I can't seem to set an object variable to an integer or single whose value is zero
I have a class X with a property called NumericEntry. The property is of the object type since I don't know what type of value will be put in it. The property default is Nothing. It could be an integer or single. I have an issue when trying to store an integer or a single (whose value is 0) into the property. It remains Nothing. If I pass any other value as integer or single, it works fine.
Code:
Public Class X
Public Property NumericEntry as Object = Nothing
End Class
Public Sub StoreValue(Mode as Integer, Value as String)
Select Case Mode
case 1 ' integer
Dim intValue as Integer = 0
' intValue = CType(Math.Truncate(Val(Value)), Integer)
x.NumericEntry = intValue
case 2 ' single
Dim sngValue as Single = 0
' sngValue = CType(Val(Value), Single)
x.NumericEntry = sngValue
End select
End Sub
Re: I can't seem to set an object variable to an integer or single whose value is zer
Everything is working fine. The issue is your understanding of what Nothing is. Nothing is the default value for a variable. Basically, Nothing means the variable's memory is filled with zero bits. For all reference types, Nothing corresponds to a null reference. For all value types, Nothing corresponds to the default value for that type. For numeric types (Byte, Short, Integer, Long, Single, Double, Decimal) the default value is zero. If you assign a zero value of any numeric type to that Object variable and then test whether the variable is Nothing, it will be. The same goes for the Boolean value False, the Date value #1/01/0001 12:00 AM# and any other value type default values.
Re: I can't seem to set an object variable to an integer or single whose value is zer
Well, I need to get the value back out later, and when I try to read the value, I want to get a value of zero. Instead I get a null reference exception. Should I be testing the variable to see if it is nothing first and assuming that if it is nothing that means it has been set to zero? I guess there is no way to know whether it was set to zero or never set to anything.
Re: I can't seem to set an object variable to an integer or single whose value is zer
If it's not working then you're doing it wrong. As you haven't actually shown us what you're doing, we can't tell you what's wrong with it. If you run this code you'll see that there's no way that you should be getting a NullReferenceException when puling an Integer or Single from an Object variable, even if you have in fact assigned Nothing to the variable.
vb.net Code:
Module Module1
Sub Main()
Dim number As Object
number = Nothing
Console.WriteLine(CInt(number))
number = 123
Console.WriteLine(CInt(number))
number = 0
Console.WriteLine(CInt(number))
number = 456
Console.WriteLine(CInt(number))
number = Nothing
Console.WriteLine(CInt(number))
number = Nothing
Console.WriteLine(CSng(number))
number = 12.34F
Console.WriteLine(CSng(number))
number = 0.0F
Console.WriteLine(CSng(number))
number = 56.78F
Console.WriteLine(CSng(number))
number = Nothing
Console.WriteLine(CSng(number))
Console.ReadLine()
End Sub
End Module
As I said, Nothing corresponds to zero for numeric types so if you convert a null reference to a numeric type then you get zero.
If you're always going to be storing an Integer or a Single then why not just declare the variable as type Single? You'll always be able to store an Integer in it. If you want to be able to distinguish a value that should be an Integer rather than a Single then use a Boolean flag. You'd need that when using Object anyway. Also, if you actually use a value type then you could declare it as nullable, which would then allow you to distinguish between zero and no value.