Enum value nothing

  • Thread starter Thread starter Nelson Wu
  • Start date Start date
N

Nelson Wu

Hi

I have an enum type VehStatus defined in my VB.NET 2003 application as
Public Enum VehStatus
Appraisal
OnOrder
InStock
Pending
Sold
End Enum

Dim myVehStatus as VehStatus

When I was trying to set
myVehStatus = Nothing

it gave me value Appraisal instead of Nothing.

It seems Enum variable doesn't accept Nothing as its value.
If it doesn't accept nothing as its value, why doesn't IDE give me compile
error on statement
myVehStatus = Nothing (I have all options set ON).

Can anybody lighten me.

Thanks in advance.

Nelson Wu
 
Nelson said:
Hi

I have an enum type VehStatus defined in my VB.NET 2003 application as
Public Enum VehStatus
Appraisal
OnOrder
InStock
Pending
Sold
End Enum

Dim myVehStatus as VehStatus

When I was trying to set
myVehStatus = Nothing

it gave me value Appraisal instead of Nothing.

It seems Enum variable doesn't accept Nothing as its value.
If it doesn't accept nothing as its value, why doesn't IDE give me compile
error on statement
myVehStatus = Nothing (I have all options set ON).

Can anybody lighten me.

The default value of the first enumeration member is 0.

When you assign Nothing to a value type, then it's set to 0.

You can fix your problem by changing the enum to:

Public Enum VehStatus
Appraisal = 1
OnOrder
InStock
Pending
Sold
End Enum
 
Thanks, it is a great help
Nelson

mikeb said:
The default value of the first enumeration member is 0.

When you assign Nothing to a value type, then it's set to 0.

You can fix your problem by changing the enum to:

Public Enum VehStatus
Appraisal = 1
OnOrder
InStock
Pending
Sold
End Enum
 
What would happen if you turned strict on. It seems setting a value type to nothing is not valid. It isnt in C#

Regard
Michael
 
Michael said:
What would happen if you turned strict on. It seems setting a value
type to nothing is not valid. It isnt in C#.

No, but that's because it isn't a reference type, so null (the nearest
equivalent of VB.NET's Nothing for most purposes) isn't appropriate.
What you *can* do in C# is:

MyEnum foo = 0;

which is *really* the equivalent of setting it to Nothing in VB.NET.
 
Michael said:
What would happen if you turned strict on. It seems setting a value type to nothing is not valid. It isnt in C#.

The definition of Nothing in VB.NET is different than the definition of
null in C#.

VB.NET's Nothing can be assigned to any variable - value type or
reference type. For value types it assigns 0 (for structures, it will
zero all members), for reference types it assigns null.

Option Strict has no effect on this.
 
Back
Top