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




Reply With Quote
