Solved

ASP.NET Random color selector algorithm

Posted on 2004-04-22
4
923 Views
Last Modified: 2008-02-01
In ASP.NET, there is a Color enumeration.

I would like to be able to programmatically randomly select a color name during a postback event, and use it to build into a string to create a "template" in a textbox.

When posted back, a panel control is made visible and inside the panel is a textbox. The textbox will come pre-filled in with :

color is [color]    where [color] is a randomly chosen color name from the Color enumeration.

I know one approach is the tedius way of making an array of all the cololr names and then using the random object to come up with a number. But what I am really look for from you experts is a more elegant and lazy approach.... THANKS..
0
Comment
Question by:gregasm
  • 3
4 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10896652
The KnownColor values are already in the enum, so you can create the array on the fly

Private Function GetRandomColor() As KnownColor

        Dim a As Array
        Dim r As Random = New Random
        Dim iElem As Integer

        a = [Enum].GetValues(GetType(KnownColor))

        iElem = r.Next(0, a.Length() - 1)
        Return a.GetValue(iElem)

    End Function
0
 
LVL 12

Accepted Solution

by:
dfiala13 earned 500 total points
ID: 10903557
Or for the truly lazy...

    Private Function GetRandomColor() As Color

        Dim a As Array
        Dim r As Random = New Random
        Dim iElem As Integer

        a = [Enum].GetValues(GetType(KnownColor))

        iElem = r.Next(0, a.Length() - 1)
        Return Color.FromKnownColor(a.GetValue(iElem))

    End Function
0
 
LVL 8

Author Comment

by:gregasm
ID: 10909591
Thanks for your excellent answer, dfiala13!

This is the c# equivalent::

private Color getRandomColor()
{
      Array a;
      Random r = new Random();
      int iElem;
      a = Enum.GetValues(typeof(KnownColor));
      iElem = r.Next(0, a.Length - 1);
      return Color.FromKnownColor((KnownColor)a.GetValue(iElem));
}
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10909876
Thanks.

You didn't mention what language you were interested in...
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question