Re: Enumerations And Random Numbers

From: Herfried K. Wagner [MVP] (hirf-spam-me-here_at_gmx.at)
Date: 10/30/04


Date: Sat, 30 Oct 2004 12:54:16 +0200


"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> schrieb:
> If I have an enumeration such as this
>
> Fiend Enum TravelDirection
> North
> South
> East
> West
> End Enum
>
> and
>
> Direction = TravelDirection
>
> Now I want to set the Direction Randomly to one of the TravelDirection
> enum values
>
> If I know that my enum is between 1 and 4 I can easily get a new random
> number and assign it, however, As I add to the enumeration, I wont want to
> worry about coding all the changes, so how could I easily do this I know
> I can do it, and I have but Im looking for the most simple solution.

\\\
Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles))
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())
///

-- 
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>