Strange Enum Properties

R

roel.schreurs

When I make an enumeration, the various values are accessed by
<enum_name>.<option1>. I noticed that each option has properties that
return any of the options in the enumeration, as can easily seen by
listing the members (typing a . after the expression). This way, you
can make expressions like:
<enum_name>.<option1>.<option2>.<option3>.<option1> e.t.c. The
expression takes on the value of the last option.

I was wondering what purpose these properties have. Does anybody know
this?

Below is an example made on an empty VB.NET Windows application. It
compiles and runs (doing very little of course).

'----------------------------
Option Strict On
Option Explicit On
Public Class Form1
Inherits System.Windows.Forms.Form

'(#Region " Windows Form Designer generated code ")

Private Enum MyEnum
FirstOption
SecondOption
ThirdOption
End Enum
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim test As MyEnum = MyEnum.FirstOption
Select Case test

Case
MyEnum.FirstOption.SecondOption.ThirdOption.ThirdOption.ThirdOption.FirstOption

Case MyEnum.SecondOption

Case MyEnum.ThirdOption

End Select

End Sub
End Class
 
K

Kalpesh

Internally, an enum is written as struct

foe g
Enum Color
{
Red
Green
Blue
}

will be considered as

Struct Color
{
public int value;

public const Color Red = 0;
public const Color Blue = 1;
public const Color Green = 2;
}

Hence, you can write Color.Red.Blue.Green which will turn out to be
Color.Red.Blue.Green.value (i.e. 2)

Does this help ?
Kalpesh
 
G

Guest

Actually, it's a little different.
With that structure definition, you couldn't have:
Dim test As Color = Color.Red Then ...

You'd need:
Dim test As Color
test.value = Color.Red

So, an enum is similar to a struct but allows access to the 'value' property
by default.

Also, the fact that you can access the various values from each other is
just due to a feature (or quirk)
in VB which allows you to access shared items from instance members.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top