Results 1 to 4 of 4

Thread: I can't seem to set an object variable to an integer or single whose value is zero

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2016
    Posts
    21

    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

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,008

    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.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2016
    Posts
    21

    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.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,008

    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:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim number As Object
    5.  
    6.         number = Nothing
    7.         Console.WriteLine(CInt(number))
    8.         number = 123
    9.         Console.WriteLine(CInt(number))
    10.         number = 0
    11.         Console.WriteLine(CInt(number))
    12.         number = 456
    13.         Console.WriteLine(CInt(number))
    14.         number = Nothing
    15.         Console.WriteLine(CInt(number))
    16.  
    17.         number = Nothing
    18.         Console.WriteLine(CSng(number))
    19.         number = 12.34F
    20.         Console.WriteLine(CSng(number))
    21.         number = 0.0F
    22.         Console.WriteLine(CSng(number))
    23.         number = 56.78F
    24.         Console.WriteLine(CSng(number))
    25.         number = Nothing
    26.         Console.WriteLine(CSng(number))
    27.  
    28.         Console.ReadLine()
    29.     End Sub
    30.  
    31. 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.
    Last edited by jmcilhinney; Oct 6th, 2016 at 09:44 PM. Reason: Added missing code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width