By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,417 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,417 IT Pros & Developers. It's quick & easy.

Strange Enum Properties

P: n/a
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.ThirdO ption.ThirdOption.FirstOption

Case MyEnum.SecondOption

Case MyEnum.ThirdOption

End Select

End Sub
End Class

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Nov 21 '05 #2

P: n/a
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
"Kalpesh" wrote:
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

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.