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