Setting Integers or Enumerations to Nothing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DippyDog

    #1

    Setting Integers or Enumerations to Nothing

    This is an old old post that I'm referencing regarding what happens
    when you set an integer variable to Nothing. It gets set to zero, not
    "Nothing."



    Just to expand the understanding of the problems this behavior can
    cause, because a typical enumeration (enum) is an integer, setting an
    enum variable to Nothing actually sets it to it's first item...

    Enum TestEnum
    A
    B
    C
    End Enum

    Sub SetEnum()
    Dim te as New TestEnum 'value is TestEnum.A
    te = TestEnum.B 'value of te is now TestEnum.B
    te = Nothing 'value of te is now back to TestEnum.A
    End Sub

    I pulled my hair out over this one. Fortunately (most of) it grew
    back. A way around this is to set the values of the enum members
    manually...

    Enum TestEnum
    A = 1
    B = 2
    C = 3
    End Enum

    Now setting a variable of this enum type to Nothing will still set it
    to zero, but it's not longer in the defined list so a particular
    select-case, for example, won't be erroneously processed. Lookout,
    though, as this can be dangerous in itself. In my case, one object
    raises an event passing two TestEnum values-- a current value and a
    previous value. For the first event, there is no previous value, so I
    set it to Nothing. But I have to be careful what I do with that
    "Nothing" (actually zero) in any handler. Any thoughts on how to do
    this better? Am I making this more difficult than it is? Do I need to
    find a different profession?
    thanks and Merry Christmas.
  • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

    #2
    RE: Setting Integers or Enumerations to Nothing



    "DippyDog" wrote:
    This is an old old post that I'm referencing regarding what happens
    when you set an integer variable to Nothing. It gets set to zero, not
    "Nothing."
    >

    >
    Just to expand the understanding of the problems this behavior can
    cause, because a typical enumeration (enum) is an integer, setting an
    enum variable to Nothing actually sets it to it's first item...
    >
    Enum TestEnum
    A
    B
    C
    End Enum
    >
    Sub SetEnum()
    Dim te as New TestEnum 'value is TestEnum.A
    te = TestEnum.B 'value of te is now TestEnum.B
    te = Nothing 'value of te is now back to TestEnum.A
    End Sub
    >
    I pulled my hair out over this one. Fortunately (most of) it grew
    back. A way around this is to set the values of the enum members
    manually...
    >
    Enum TestEnum
    A = 1
    B = 2
    C = 3
    End Enum
    >
    Now setting a variable of this enum type to Nothing will still set it
    to zero, but it's not longer in the defined list so a particular
    select-case, for example, won't be erroneously processed. Lookout,
    though, as this can be dangerous in itself. In my case, one object
    raises an event passing two TestEnum values-- a current value and a
    previous value. For the first event, there is no previous value, so I
    set it to Nothing. But I have to be careful what I do with that
    "Nothing" (actually zero) in any handler. Any thoughts on how to do
    this better? Am I making this more difficult than it is? Do I need to
    find a different profession?
    thanks and Merry Christmas.
    >
    No you don't need a different profession, I don't think...

    Just don't equate enums and integers. Also, in your condition checks such
    as a select,
    make sure you include "Case Else". You could ignore this case, or do
    something like pop
    up a message. This allows you to catch conditions later when you or another
    developer adds a new enum value.


    Comment

    • Tom Dacon

      #3
      Re: Setting Integers or Enumerations to Nothing

      You should know that integers and enumerations are what are called "value
      types" in .Net, and cannot actually assume the value of Nothing. VB sort of
      gives you a free pass on the assignment, by not pulling a compile error, but
      it's not actually a valid assignment. Under the hood, VB just assigns it a
      suitable value instead. You've already seen what choices VB makes in those
      cases.

      So you should adjust your coding style to explicitly assign integer or
      enumeration values you want the variable to hold. A useful suggestion that
      FXCop will offer you is to add an enumerated value of zero, with the
      symbolic value of "None", and assign it when you want the enumeration to
      hold something besides any other value. For integers, it's up to you as to
      what is an invalid value (if there is such a thing for the specific variable
      that you're assigning).

      This is the case in .Net 2.0, anyway. When you get up to 3.0 or 3.5 (I'm not
      sure which) there are nullable types that you can use instead of the bare
      integers and enumerations. I'm still on 2.0, so I can't comment further.

      HTH,
      Tom Dacon
      Dacon Software Consulting

      "DippyDog" <dippykdog@gmai l.comwrote in message
      news:62f9cf80-2a0d-4ba0-b86c-b3ab062335d3@c4 g2000hsg.google groups.com...
      This is an old old post that I'm referencing regarding what happens
      when you set an integer variable to Nothing. It gets set to zero, not
      "Nothing."
      >

      >
      Just to expand the understanding of the problems this behavior can
      cause, because a typical enumeration (enum) is an integer, setting an
      enum variable to Nothing actually sets it to it's first item...
      >
      Enum TestEnum
      A
      B
      C
      End Enum
      >
      Sub SetEnum()
      Dim te as New TestEnum 'value is TestEnum.A
      te = TestEnum.B 'value of te is now TestEnum.B
      te = Nothing 'value of te is now back to TestEnum.A
      End Sub
      >
      I pulled my hair out over this one. Fortunately (most of) it grew
      back. A way around this is to set the values of the enum members
      manually...
      >
      Enum TestEnum
      A = 1
      B = 2
      C = 3
      End Enum
      >
      Now setting a variable of this enum type to Nothing will still set it
      to zero, but it's not longer in the defined list so a particular
      select-case, for example, won't be erroneously processed. Lookout,
      though, as this can be dangerous in itself. In my case, one object
      raises an event passing two TestEnum values-- a current value and a
      previous value. For the first event, there is no previous value, so I
      set it to Nothing. But I have to be careful what I do with that
      "Nothing" (actually zero) in any handler. Any thoughts on how to do
      this better? Am I making this more difficult than it is? Do I need to
      find a different profession?
      thanks and Merry Christmas.

      Comment

      • Tom Shelton

        #4
        Re: Setting Integers or Enumerations to Nothing

        On 2007-12-18, Tom Dacon <tdacon@communi ty.nospamwrote:
        You should know that integers and enumerations are what are called "value
        types" in .Net, and cannot actually assume the value of Nothing. VB sort of
        gives you a free pass on the assignment, by not pulling a compile error, but
        it's not actually a valid assignment. Under the hood, VB just assigns it a
        suitable value instead. You've already seen what choices VB makes in those
        cases.
        >
        So you should adjust your coding style to explicitly assign integer or
        enumeration values you want the variable to hold. A useful suggestion that
        FXCop will offer you is to add an enumerated value of zero, with the
        symbolic value of "None", and assign it when you want the enumeration to
        hold something besides any other value. For integers, it's up to you as to
        what is an invalid value (if there is such a thing for the specific variable
        that you're assigning).
        >
        This is the case in .Net 2.0, anyway. When you get up to 3.0 or 3.5 (I'm not
        sure which) there are nullable types that you can use instead of the bare
        integers and enumerations. I'm still on 2.0, so I can't comment further.
        VB.NET 2005 has nullable types...

        --
        Tom Shelton

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Setting Integers or Enumerations to Nothing

          "Tom Shelton" <tom_shelton@YO UKNOWTHEDRILLco mcast.netschrie b:
          >You should know that integers and enumerations are what are called "value
          >types" in .Net, and cannot actually assume the value of Nothing. VB sort
          >of
          >gives you a free pass on the assignment, by not pulling a compile error,
          >but
          >it's not actually a valid assignment. Under the hood, VB just assigns it
          >a
          >suitable value instead. You've already seen what choices VB makes in
          >those
          >cases.
          >>
          >So you should adjust your coding style to explicitly assign integer or
          >enumeration values you want the variable to hold. A useful suggestion
          >that
          >FXCop will offer you is to add an enumerated value of zero, with the
          >symbolic value of "None", and assign it when you want the enumeration to
          >hold something besides any other value. For integers, it's up to you as
          >to
          >what is an invalid value (if there is such a thing for the specific
          >variable
          >that you're assigning).
          >>
          >This is the case in .Net 2.0, anyway. When you get up to 3.0 or 3.5 (I'm
          >not
          >sure which) there are nullable types that you can use instead of the bare
          >integers and enumerations. I'm still on 2.0, so I can't comment further.
          >
          VB.NET 2005 has nullable types...
          ..NET Framework 2.0 has nullable types, which are simply implemented as a
          generic class 'Nullable(Of T)'. VB 2008 will provide the syntactical sugar
          which makes using nullable types easier ('Date?' instead of 'Nullable(Of
          Date)' etc.).

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

          Comment

          • Tom Shelton

            #6
            Re: Setting Integers or Enumerations to Nothing

            On 2007-12-18, Herfried K. Wagner [MVP] <hirf-spam-me-here@gmx.atwrot e:
            "Tom Shelton" <tom_shelton@YO UKNOWTHEDRILLco mcast.netschrie b:
            >>You should know that integers and enumerations are what are called "value
            >>types" in .Net, and cannot actually assume the value of Nothing. VB sort
            >>of
            >>gives you a free pass on the assignment, by not pulling a compile error,
            >>but
            >>it's not actually a valid assignment. Under the hood, VB just assigns it
            >>a
            >>suitable value instead. You've already seen what choices VB makes in
            >>those
            >>cases.
            >>>
            >>So you should adjust your coding style to explicitly assign integer or
            >>enumeration values you want the variable to hold. A useful suggestion
            >>that
            >>FXCop will offer you is to add an enumerated value of zero, with the
            >>symbolic value of "None", and assign it when you want the enumeration to
            >>hold something besides any other value. For integers, it's up to you as
            >>to
            >>what is an invalid value (if there is such a thing for the specific
            >>variable
            >>that you're assigning).
            >>>
            >>This is the case in .Net 2.0, anyway. When you get up to 3.0 or 3.5 (I'm
            >>not
            >>sure which) there are nullable types that you can use instead of the bare
            >>integers and enumerations. I'm still on 2.0, so I can't comment further.
            >>
            >VB.NET 2005 has nullable types...
            >
            .NET Framework 2.0 has nullable types, which are simply implemented as a
            generic class 'Nullable(Of T)'. VB 2008 will provide the syntactical sugar
            which makes using nullable types easier ('Date?' instead of 'Nullable(Of
            Date)' etc.).
            >
            Right - but, you can still make use of them in 2005. To be honest, I
            haven't been playing with VB2008 yet... So, do you also get the ??
            operator as well?

            --
            Tom Shelton

            Comment

            • =?Utf-8?B?RGF2aWQgQW50b24=?=

              #7
              Re: Setting Integers or Enumerations to Nothing

              You're confusing 'Nothing' with 'null' in C#.
              'Nothing' in VB is more general than 'null' in C# and means 'the default
              value' in the context.
              You can assign and compare value types in VB to 'Nothing' (when comparing
              you use '=' instead of 'Is' with value types).

              It gets more confusing with strings where "= Nothing" and "Is Nothing" both
              work and produce sometimes different results, but that's another topic...
              --
              Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

              C++ to C#
              C++ to VB
              C++ to Java
              C++ to Ruby
              Instant C#: VB to C# converter
              Instant VB: C# to VB converter
              Instant C++: convert VB or C# to C++/CLI


              "Tom Dacon" wrote:
              You should know that integers and enumerations are what are called "value
              types" in .Net, and cannot actually assume the value of Nothing. VB sort of
              gives you a free pass on the assignment, by not pulling a compile error, but
              it's not actually a valid assignment. Under the hood, VB just assigns it a
              suitable value instead. You've already seen what choices VB makes in those
              cases.
              >
              So you should adjust your coding style to explicitly assign integer or
              enumeration values you want the variable to hold. A useful suggestion that
              FXCop will offer you is to add an enumerated value of zero, with the
              symbolic value of "None", and assign it when you want the enumeration to
              hold something besides any other value. For integers, it's up to you as to
              what is an invalid value (if there is such a thing for the specific variable
              that you're assigning).
              >
              This is the case in .Net 2.0, anyway. When you get up to 3.0 or 3.5 (I'm not
              sure which) there are nullable types that you can use instead of the bare
              integers and enumerations. I'm still on 2.0, so I can't comment further.
              >
              HTH,
              Tom Dacon
              Dacon Software Consulting
              >
              "DippyDog" <dippykdog@gmai l.comwrote in message
              news:62f9cf80-2a0d-4ba0-b86c-b3ab062335d3@c4 g2000hsg.google groups.com...
              This is an old old post that I'm referencing regarding what happens
              when you set an integer variable to Nothing. It gets set to zero, not
              "Nothing."



              Just to expand the understanding of the problems this behavior can
              cause, because a typical enumeration (enum) is an integer, setting an
              enum variable to Nothing actually sets it to it's first item...

              Enum TestEnum
              A
              B
              C
              End Enum

              Sub SetEnum()
              Dim te as New TestEnum 'value is TestEnum.A
              te = TestEnum.B 'value of te is now TestEnum.B
              te = Nothing 'value of te is now back to TestEnum.A
              End Sub

              I pulled my hair out over this one. Fortunately (most of) it grew
              back. A way around this is to set the values of the enum members
              manually...

              Enum TestEnum
              A = 1
              B = 2
              C = 3
              End Enum

              Now setting a variable of this enum type to Nothing will still set it
              to zero, but it's not longer in the defined list so a particular
              select-case, for example, won't be erroneously processed. Lookout,
              though, as this can be dangerous in itself. In my case, one object
              raises an event passing two TestEnum values-- a current value and a
              previous value. For the first event, there is no previous value, so I
              set it to Nothing. But I have to be careful what I do with that
              "Nothing" (actually zero) in any handler. Any thoughts on how to do
              this better? Am I making this more difficult than it is? Do I need to
              find a different profession?
              thanks and Merry Christmas.
              >
              >
              >

              Comment

              Working...