How to "increment" an Enum?

B

Bob Altman

Hi all,

I have an Enum declared (in VB) as:

Private Enum State
First
Second
Third
End Enum

If I have an instance of this enum, how can I "increment" it? For example:

Dim currentState as State = State.First ' currentState is 0
currentState = <some expression yielding State.Second, i.e. 1>

TIA,

- Bob
 
T

Tom Dacon

Here's what I started with:

State startWith = State.First;
State next = (State)((int)(startWith) + 1);

but then just on a whim I tried this:

State startWith = State.First;
State next = startWith + 1;

and it worked just as well (at least on VS2003).

However you do it, you'll probably want to supply a little code to make sure
that it doesn't get incremented 'off the end' of the enumeration.

HTH,
Tom Dacon
 
T

Tom Dacon

Sorry about the C# example; I went back and read your original post and saw
that it was in VB.Net. Under Option Strict in VB.Net, the following line of
code works:

nextState = Ctype(CType(currentState, integer) + 1, State)

HTH,
Tom Dacon
Dacon Software Consulting
 
J

John Saunders

Tom Dacon said:
Here's what I started with:

State startWith = State.First;
State next = (State)((int)(startWith) + 1);

but then just on a whim I tried this:

State startWith = State.First;
State next = startWith + 1;

and it worked just as well (at least on VS2003).

Does that still work with Options Strict turned On?
 
B

Bob Altman

Thanks Tom. I was looking for some magic method on either the Convert class
or the Enum class. It never occurred to me to do the obvious thing and
simply cast my enum to int and back again. Thanks again!

- Bob
 
J

Jay B. Harlow [MVP - Outlook]

Bob,
Just be aware that you may wind up with an invalid Enum value, without an
exception!

Consider the following:
Dim currentState As State = State.Third
nextState will actually have the value of 3, however First = 0, Second = 1,
Third = 2. What value does 3 represent?

You can use System.Enum.IsDefined to ensure that nextState is valid.

Hope this helps
Jay
 
B

Bob Altman

Thanks!

- Bob

Jay B. Harlow said:
Bob,
Just be aware that you may wind up with an invalid Enum value, without an
exception!

Consider the following:
Dim currentState As State = State.Third
nextState will actually have the value of 3, however First = 0, Second = 1,
Third = 2. What value does 3 represent?

You can use System.Enum.IsDefined to ensure that nextState is valid.

Hope this helps
Jay
 
T

Tom Dacon

John, the code below is C#, which doesn't have the notion of Option Strict;
see my own response to my message that contained this code, in which I show
a line of VB that works with Option Strict on. Sorry about the C# confusion;
I wasn't thinking straight when I made my first response.

Tom Dacon
 
P

Peter Huang

Hi Bob,

I agree with Tom and Jay's suggestion, based on my knowledge, so far we
need to convert the enum to int which supported the add operation to do the
increase stuff on the enum type.



Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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