Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Switch with AppSettings

Status
Not open for further replies.

Elegabalus

Programmer
Jan 13, 2005
71
CA
I'm using the Visual Studio 2005 Beta 2 build, but this should be the same across different versions.

I'm trying to use a Switch/Case statement to determine which label should be displayed.

I've got several keys stored in the web.config file that I want to use as the comparison for the switch statement. However, I get an error saying that "A constant value is expected." I understand that you can't have variables in the switch statement, but you can't have a fixed value from the config file?

Here's some code:

Code:
switch (strID)
{
	case ConfigurationManager.AppSettings["ID1"].ToString():
		Label1.Visible = true;
		break;
	case ConfigurationManager.AppSettings["ID2"].ToString():
		Label2.Visible = true;
		break;
	case ConfigurationManager.AppSettings["ID3"].ToString():
		Label3.Visible = true;
		break;
	default:
		Label1.Visible = true;
		break;
}

Anyone have an idea how to get this to work?
 
You could load the variables into an array and then loop through them until you find the correct one and then set some sort of flag/enumeration based on which item was found.

public enum IDType { zero, one, two, three }


ArrayList ids = new ArrayList();

ids.Add(ConfigurationManager.AppSettings["ID1"].ToString()):
ids.Add(ConfigurationManager.AppSettings["ID2"].ToString()):
ids.Add(ConfigurationManager.AppSettings["ID3"].ToString()):

IDType type = IDType.zero;

for (int i = 0; i < ids.Count; i++)
{
if (strID == (string)ids)
{
type = IDType.indexof(i); //Don't remember this but...
}
}

switch (type)
{
case IDType.one:
{
//dostuff
break;
}
}


bit of a long route, but it might give you some ideas.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top