473,692 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18042
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******** *************@g 47g2000cwa.goog legroups.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.co m

"Brett Romero" <ac*****@cygen. com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.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******** *************@g 47g2000cwa.goog legroups.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.co m

"Brett Romero" <ac*****@cygen. com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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.goo glegroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10948
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 ended up using .createProcessor and doing all the work behind the scenes in my programming language, then using the
6
4696
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 <iomanip> #include <iostream> using namespace std;
2
2998
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' THEN '02' WHEN IPVIS = 'Y' THEN '03' ELSE ' ' END AS CRIT_CODE,
12
21075
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 record (3232,'Raindeer') based on the condition that the
4
2802
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 element NOVEMBER.
1
21677
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 "01" : dWarrExpDateMonth="Jan" : dRetailDateMonth="Jan" case "02" : dWarrExpDateMonth="Feb" : dRetailDateMonth="Feb" End Select
3
1464
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 THEN City1 WHEN 8030000000 THEN City2
2
1782
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 wondering if i should be using if logic. Ideas and examples appreciated.
13
11813
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 just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size can be reduced?? Thanks in advance.
5
7299
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: http://bytes.com/topic/c/answers/61787-const-int-not-constant-expression Nevertheless all the provided solutions don't work for me (except using #define apparently). If I just have a simple program like: int main( int argc, char** argv ) { int...
0
8610
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8810
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8810
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6462
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5822
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4327
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4562
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2978
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2242
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.