473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to use variable in case statement?

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
10 18017
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
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
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

"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
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

"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
Pefect! Thanks. const did it.

Brett

Feb 7 '06 #8
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gadrin77 | last post by:
as a newbie to XSL, is it possible to mimic a SELECT/CASE statement using XSL? I tried a quickie and I kept getting errors either using PARAM or WITH-PARAM in the wrong place or VARIABLE. I...
6
by: deanfamily11 | last post by:
I've set up a case statement to have my program determine where on the Cartesian plane a point the user enters is located. I keep getting the C2051 error when I compile. Any help? #include...
2
by: deanclowe | last post by:
Hi I have a query that is trying to use a field that is created "AS" based on a case statement like this: SELECT CASE WHEN IPOVH = 'Y' THEN '01' WHEN IPHSI = 'Y' ...
12
by: rAinDeEr | last post by:
Hi, I have a table with 2 columns ** CREATE TABLE test (emp_num DECIMAL(7) NOT NULL,emp_name CHAR(10) NOT NULL) and i have inserted a number of records. ** Now, I want to insert a new...
4
by: vsgdp | last post by:
Why can't I define variables in a case statement: case 1: float u = expression; ... break; Also, is it bad form to cast ints to enums? For example, one might want 11 to map to an enum...
1
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case...
3
by: dhutton | last post by:
Hello How would I go about using the CASE statement with the WHEN and THEN statements with a scenario like this: ======================================--- CASE PhoneNumber WHEN 9990000000...
2
by: elpeak | last post by:
I have a stored proc that contains 10 other stored procs. When i pass in the variable i want it to find the stored proc that matches and execute. I am having issues w/ the case statement and am...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
5
by: Harry2o | last post by:
I stumbled upon the gcc error "case label does not reduce to an integer constant" when trying to use a const int variable in a case statement. Basically what is discussed in this thread:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.