Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.NET Random color selector algorithm

Posted on 2004-04-22
4
Medium Priority
?
970 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 2000 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Loops Section Overview

972 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