How to use variable in case statement?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
B

Brett Romero

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
 
Enums cannot be defined as strings, so can you clarify your statement "...
value from the same exact enums presented above"?
 
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.
 
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
 
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.
 
Brett Romero said:
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.
 
Brett Romero said:
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.
 
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
 
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?
 
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top