I'm trying to set a variable to a color by using a switch statement to evaluate a string. Here's the code: private void DrawBarChart(Graphics g, String theYear, String theOption, String theBG, String theBar) { Color clrBack = Color.LightYellow; Color clrBar
= Color.Red; Switch(theBG) { case("LightYellow"): clrBack = Color.LightYellow; break; case("White"): clrBack = Color.White; break; case("LightGray"): clrBack = Color.LightGray; break; default: clrBack = Color.LightYellow; break; } Switch(theBar) { case("Red"):
clrBar = Color.Red; break; case("Blue"): clrBar = Color.Blue; break; case("Green"): clrBar = Color.Green; break; case("Purple"): clrBar = Color.Purple; break; default: clrBar = Color.Red; break; } If I comment out the switch statements the code will run with
the default values defined at the beginning. I don't see what is wrong. Anyone have an idea?
I figured it out...I was using Switch vs switch. I put the code in VS.Net and figured it out. switch(theBG) { case "LightYellow": ... case "White": ... default: ... }
Try this it is much cleaner. Color clrBack; try { clrBack = (Color)Color.FromName(theBG); } catch (Exception) { ... Do Default Stuff Here ... } This will work for any color that is listed in the Color Struct
Hawkeye3677
Member
10 Points
2 Posts
Setting variable with switch stmt
Dec 11, 2003 02:49 PM|LINK
Hawkeye3677
Member
10 Points
2 Posts
Re: Setting variable with switch stmt
Dec 11, 2003 04:49 PM|LINK
nberardi
Star
11233 Points
2352 Posts
Re: Setting variable with switch stmt
Dec 15, 2003 03:34 PM|LINK