Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have the folowing :
C#
string na = system.environment.machineName;
switch (na)
{
case FIRST VAIRABLE :
{
satament
}

case SECOUND VAIRABLE : 
{
satament
}
} 


the qustion: how can I use vairable with case , when I try it I found the following message ( A constant value is expected)
best regards
Posted
Updated 30-Aug-11 1:30am
v2
Comments
Philippe Mori 30-Aug-11 8:41am    
As the compiler tell you... it is illegal to uses a variable. You must uses something else than a switch statement in that case. Many people already told you. Stop asking!

You can't use a variable. You have to use a constant, like so:

C#
const string XYZ = "2ndVariable";

switch (na)
{
    case "1stVariable" : 
        break;
    case XYZ :
        break;
}

You can use any intrinsic type in a switch statement, and despite my examle above, it's probably not wise to mix literals and constants because you'll invariably duplicate a constant in the form of a literal, and intellisense will complain.
 
Share this answer
 
Comments
Wayne Gaylard 30-Aug-11 7:32am    
You beat me to it.:) Have 5.
#realJSOP 30-Aug-11 7:38am    
That's twice in two days, isn't it? :)
Wayne Gaylard 30-Aug-11 7:42am    
Is it?
RaisKazi 30-Aug-11 7:39am    
My 5.
somur-ruteeb 30-Aug-11 7:54am    
but variable1 not const I get this vairable from txtfile as following:
StreamReader streamReaderFrom = new StreamReader(".\\From.txt");
variable1 = streamReaderFrom.ReadToEnd();
streamReaderFrom.Close();
I assume you mean something like this

C#
string x = "MyMachine";
            string y = "YourMachine";
            string na = System.Environment.MachineName;
            switch (na)
            {
                case x:

            }


I am afraid this is not possible. You will have to supply the proper string like this:

C#
string na = System.Environment.MachineName;
           switch (na)
           {
               case "MyMachine":
                   //do something
                   break;
               case "YourMachine":
                   //do something else
                   break;

           }


If you could tell us more about what you would like to achieve we might be able to help more.

In answer to your comment, I would change this to use an If construct, something like this:

C#
StreamReader srFrom = new StreamReader(@"your file path");
            string na = System.Environment.MachineName;
            string name = srFrom.ReadLine();
            srFrom.Close();
            if (na == name)
            {
                //Do something
            }
            else
            {
                //Do something else
            }


Hope this helps
 
Share this answer
 
v2
Comments
RaisKazi 30-Aug-11 7:40am    
My 5. You both beat me :).
somur-ruteeb 30-Aug-11 7:53am    
but variable1 not const I get this vairable from txtfile as following:
StreamReader streamReaderFrom = new StreamReader(".\\From.txt");
variable1 = streamReaderFrom.ReadToEnd();
streamReaderFrom.Close();
Wayne Gaylard 30-Aug-11 8:22am    
Please see my updated answer
That is how switch condition works in C#. It expects constant value for case.

In case if you declare your variables as constant as shown in below code, Then it won't give you error, But it will make no ddifference with hard coded values.

C#
string na = System.Environment.MachineName;
const string variable1 = "ABC";
const string variable2 = "XYZ";
switch (na)
{
    case variable1:
        //Come code
        break;
    case variable2:
        //Come other code
        break;
}
 
Share this answer
 
Comments
somur-ruteeb 30-Aug-11 7:51am    
but variable1 not const I get this vairable from txtfile as following:
StreamReader streamReaderFrom = new StreamReader(".\\From.txt");
variable1 = streamReaderFrom.ReadToEnd();
streamReaderFrom.Close();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900