Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Object behaviour of simple data types?

Object behaviour of simple data types?

Scheduled Pinned Locked Moved Visual Basic
csharphelptutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jan Tielens
    wrote on last edited by
    #1

    Hi all, In .NET all data types (Integer, Long, String, ...) are objects. But in my opinion, these data types don't really behave like normal objects. I noticed the following behaviour: Dim l As Long Msgbox(l = Nothing) 'Results in True Msgbox(l = 0) 'Results in True Even stranger behaviour: Dim l As Long l = 0 Msgbox(l = Nothing) 'Results in True I hoped that in .NET there would be a way to determine if a variable has been set or not. For example you have a class with a ID property (long), you would implement it like this: Public Class Test Private _id As Long Public Property ID As Long Get Return _id End Get Set(Value As Long) _id = Value End Set End Property End Class Suppose you would like to have functionality in this class, to save the data. In de Save sub, you would like to check if the ID property was set or not. I hoped you could do it like this: Public Sub Save If _id = Nothing Then 'Raise error to tell the ID property was not set. Else 'Save data End If End Save Was it wrong of me to expect such behaviour, or am I wrong somewhere? Thanks, Jan

    Richard DeemingR 1 Reply Last reply
    0
    • J Jan Tielens

      Hi all, In .NET all data types (Integer, Long, String, ...) are objects. But in my opinion, these data types don't really behave like normal objects. I noticed the following behaviour: Dim l As Long Msgbox(l = Nothing) 'Results in True Msgbox(l = 0) 'Results in True Even stranger behaviour: Dim l As Long l = 0 Msgbox(l = Nothing) 'Results in True I hoped that in .NET there would be a way to determine if a variable has been set or not. For example you have a class with a ID property (long), you would implement it like this: Public Class Test Private _id As Long Public Property ID As Long Get Return _id End Get Set(Value As Long) _id = Value End Set End Property End Class Suppose you would like to have functionality in this class, to save the data. In de Save sub, you would like to check if the ID property was set or not. I hoped you could do it like this: Public Sub Save If _id = Nothing Then 'Raise error to tell the ID property was not set. Else 'Save data End If End Save Was it wrong of me to expect such behaviour, or am I wrong somewhere? Thanks, Jan

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      First, you can't check for null references with If x = Nothing ... - you have to use If x **Is** Nothing ... Second, you can't perform reference comparison on value types. If x is a value type, If x Is Nothing ... will not compile. Try something like:

      Public Class Test
      Private _id As Long
      Private _idSet As Boolean = False

      Public Property ID As Long
          Get
              Return \_id
          End Get
          Set(ByVal Value As Long)
              \_id = Value
              \_idSet = True
          End Set
      End Property
      
      Public Sub Save()
          If \_idSet Then
              'Save Data
          Else
              'Throw exception
          End If
      End Sub
      

      End Class

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups