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

Re: How to "increment" an Enum? by Tom

Tom
Fri Jun 11 19:40:45 CDT 2004

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

"Bob Altman" <rda@nospam.com> wrote in message
news:uVqjVMBUEHA.972@TK2MSFTNGP10.phx.gbl...
> 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
>
>



Re: How to "increment" an Enum? by Tom

Tom
Fri Jun 11 19:47:30 CDT 2004

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

"Tom Dacon" <Tom-@t-dacons.com> wrote in message
news:e$0YnXBUEHA.3336@TK2MSFTNGP10.phx.gbl...
> 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
>
> "Bob Altman" <rda@nospam.com> wrote in message
> news:uVqjVMBUEHA.972@TK2MSFTNGP10.phx.gbl...
> > 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
> >
> >
>
>



Re: How to "increment" an Enum? by John

John
Fri Jun 11 19:49:57 CDT 2004

"Tom Dacon" <Tom-@t-dacons.com> wrote in message
news:e$0YnXBUEHA.3336@TK2MSFTNGP10.phx.gbl...
> 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?
--
John Saunders
johnwsaundersiii at hotmail



Re: How to "increment" an Enum? by Bob

Bob
Sat Jun 12 10:34:17 CDT 2004

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

"Tom Dacon" <Tom-@t-dacons.com> wrote in message
news:%23CmnYbBUEHA.712@TK2MSFTNGP11.phx.gbl...
> 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
>
> "Tom Dacon" <Tom-@t-dacons.com> wrote in message
> news:e$0YnXBUEHA.3336@TK2MSFTNGP10.phx.gbl...
> > 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
> >
> > "Bob Altman" <rda@nospam.com> wrote in message
> > news:uVqjVMBUEHA.972@TK2MSFTNGP10.phx.gbl...
> > > 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
> > >
> > >
> >
> >
>
>



Re: How to "increment" an Enum? by Jay

Jay
Sat Jun 12 10:59:09 CDT 2004

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 = Ctype(CType(currentState, integer) + 1, State)

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

"Bob Altman" <rda@nospam.com> wrote in message
news:%23kKS2JJUEHA.808@tk2msftngp13.phx.gbl...
> 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
>
> "Tom Dacon" <Tom-@t-dacons.com> wrote in message
> news:%23CmnYbBUEHA.712@TK2MSFTNGP11.phx.gbl...
> > 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
> >
> > "Tom Dacon" <Tom-@t-dacons.com> wrote in message
> > news:e$0YnXBUEHA.3336@TK2MSFTNGP10.phx.gbl...
> > > 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
> > >
> > > "Bob Altman" <rda@nospam.com> wrote in message
> > > news:uVqjVMBUEHA.972@TK2MSFTNGP10.phx.gbl...
> > > > 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
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Re: How to "increment" an Enum? by Bob

Bob
Sat Jun 12 11:24:59 CDT 2004

Thanks!

- Bob

"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
news:ecZm6YJUEHA.3596@tk2msftngp13.phx.gbl...
> 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 = Ctype(CType(currentState, integer) + 1, State)
>
> 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
>
> "Bob Altman" <rda@nospam.com> wrote in message
> news:%23kKS2JJUEHA.808@tk2msftngp13.phx.gbl...
> > 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
> >
> > "Tom Dacon" <Tom-@t-dacons.com> wrote in message
> > news:%23CmnYbBUEHA.712@TK2MSFTNGP11.phx.gbl...
> > > 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
> > >
> > > "Tom Dacon" <Tom-@t-dacons.com> wrote in message
> > > news:e$0YnXBUEHA.3336@TK2MSFTNGP10.phx.gbl...
> > > > 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
> > > >
> > > > "Bob Altman" <rda@nospam.com> wrote in message
> > > > news:uVqjVMBUEHA.972@TK2MSFTNGP10.phx.gbl...
> > > > > 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
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Re: How to "increment" an Enum? by Tom

Tom
Sat Jun 12 11:27:30 CDT 2004


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

"John Saunders" <johnwsaundersiii@notcoldmail.com> wrote in message
news:OIcC5cBUEHA.2972@TK2MSFTNGP12.phx.gbl...
> "Tom Dacon" <Tom-@t-dacons.com> wrote in message
> news:e$0YnXBUEHA.3336@TK2MSFTNGP10.phx.gbl...
> > 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?
> --
> John Saunders
> johnwsaundersiii at hotmail
>
>



Re: How to "increment" an Enum? by v-phuang

v-phuang
Sun Jun 13 20:36:32 CDT 2004

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.