Solved

ASP.NET Random color selector algorithm

Posted on 2004-04-22
4
949 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

635 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