By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 983 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

How to use variable in case statement?

P: n/a
If I want to use:

switch (AppName)
{
case ApplicationName.App1:
loadApp1Logo();
break;
case ApplicationName.App2:
loadApp2Logo();
break;
}

I'll get a compiler error:
A constant value is expected

Which means I can't use the above enums. I don't want to hardcode the
AppName in the case statements. AppName is getting its value from the
same exact enums presented above.

I can easily get around this with If/elseif statements but the case
statements are cleaner code since I'll have a few conditions. Is there
a way to do this with case statements or some other better way?

Thanks,
Brett

Feb 6 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Enums cannot be defined as strings, so can you clarify your statement "...
value from the same exact enums presented above"?

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
If I want to use:

switch (AppName)
{
case ApplicationName.App1:
loadApp1Logo();
break;
case ApplicationName.App2:
loadApp2Logo();
break;
}

I'll get a compiler error:
A constant value is expected

Which means I can't use the above enums. I don't want to hardcode the
AppName in the case statements. AppName is getting its value from the
same exact enums presented above.

I can easily get around this with If/elseif statements but the case
statements are cleaner code since I'll have a few conditions. Is there
a way to do this with case statements or some other better way?

Thanks,
Brett

Feb 6 '06 #2

P: n/a
Brett,

Are you sure it is on AppName, and not on ApplicationName.App1 (or
App2). These need to be enumeration values, or constants. They can't be
variables (are they properties, or fields that are exposed?).

This error is for the case statements, not for the AppName. AppName can
be a constant (but that would be pointless), or a variable.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
If I want to use:

switch (AppName)
{
case ApplicationName.App1:
loadApp1Logo();
break;
case ApplicationName.App2:
loadApp2Logo();
break;
}

I'll get a compiler error:
A constant value is expected

Which means I can't use the above enums. I don't want to hardcode the
AppName in the case statements. AppName is getting its value from the
same exact enums presented above.

I can easily get around this with If/elseif statements but the case
statements are cleaner code since I'll have a few conditions. Is there
a way to do this with case statements or some other better way?

Thanks,
Brett

Feb 6 '06 #3

P: n/a
Sorry, I wasn't clear. By this, "I don't want to hardcode the AppName
in the case statements". I meant to say ApplicationName.x. The error
is on the enums and not AppName.

Here is one of the enums:

public struct ApplicationName
{
public static string App1 = "MyApp1";

So it is actually an enumeration as you suggested Nicholas. But I get
the error.

Thanks,
Brett

Feb 6 '06 #4

P: n/a

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
If I want to use:

switch (AppName)
{
case ApplicationName.App1:
loadApp1Logo();
break;
case ApplicationName.App2:
loadApp2Logo();
break;
}

I'll get a compiler error:
A constant value is expected

Which means I can't use the above enums. I don't want to hardcode the
AppName in the case statements. AppName is getting its value from the
same exact enums presented above.

I can easily get around this with If/elseif statements but the case
statements are cleaner code since I'll have a few conditions. Is there
a way to do this with case statements or some other better way?


A switch isn't going to work without constant values, an if structure is
your only good option if its only a few, stable conditions. I know its not
as clean, but the other option is overkill for 3 or 5 possiblities.

The first thing this brings to mind is why isn't there a class that has both
teh name of the application and the ability to load the logo? That looks,
from this tiny sample, like ill design. Try to use polymorphism to your
advantage.

Now, if you are going to have many, from several different sources(plugins,
xml file, whatever), you might want to look into using a dictionary to map
between the name and a delegate/interface that performs the work you want to
do. I'd still probably prefer a class, myself, but I don't know your entire
situation.

Feb 6 '06 #5

P: n/a
Brett,

What you describe is not an enumeration, not according to the framework.
You can not have static variables in your case statements as the values you
are checking AppName against.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Sorry, I wasn't clear. By this, "I don't want to hardcode the AppName
in the case statements". I meant to say ApplicationName.x. The error
is on the enums and not AppName.

Here is one of the enums:

public struct ApplicationName
{
public static string App1 = "MyApp1";

So it is actually an enumeration as you suggested Nicholas. But I get
the error.

Thanks,
Brett

Feb 6 '06 #6

P: n/a

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Sorry, I wasn't clear. By this, "I don't want to hardcode the AppName
in the case statements". I meant to say ApplicationName.x. The error
is on the enums and not AppName.

Here is one of the enums:

public struct ApplicationName
{
public static string App1 = "MyApp1";

So it is actually an enumeration as you suggested Nicholas. But I get
the error.
The enumerations he means are of type System.Enum,

enum Enumeration
{
X,
Y,
Z
}

Not in the sense you are using here. However, since you are talking about
constant values(which I didn't realize in my last response) you should be
able to do what you are doing here:
public struct ApplicationName
{
public const string App1 = "MyApp1";
replace static with const and you shouldn't have any more trouble.
Thanks,
Brett

Feb 6 '06 #7

P: n/a
Pefect! Thanks. const did it.

Brett

Feb 7 '06 #8

P: n/a
I notice that changing that enum from static to const doesn't allow me
to pass it into a particular method as a string parameter. I get an
error that the field was not found.

Any ideas why that may be?

Thanks,
Brett

Feb 8 '06 #9

P: n/a
It shouldn't make any difference, unless the parameter in question is a
"ref" or "out" parameter. But then in that case it would give you a
different error, not "not found".

Sounds like something else is going on. Can you post the relevant code?

Feb 8 '06 #10

P: n/a
It should make no difference, unless the parameter in question is "ref"
or "out", but then it wouldn't give you a "not found" message.

Sounds like something else is going on. Can you post the relevant code?

Feb 8 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.