我如何使用随身携带 [英] how can I use vairable with case

查看:85
本文介绍了我如何使用随身携带的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几点:

I have the folowing :

string na = system.environment.machineName;
switch (na)
{
case FIRST VAIRABLE :
{
satament
}

case SECOUND VAIRABLE : 
{
satament
}
} 



问题:我如何在案例中使用vairable?当我尝试时,我发现了以下消息(需要一个恒定值)
最好的问候



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

推荐答案

您不能使用变量.您必须使用常量,如下所示:

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

const string XYZ = "2ndVariable";

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


您可以在switch语句中使用任何内部类型,尽管上面有我的例子,混合文字和常量可能并不明智,因为您将始终以文字形式复制常量,而intellisense会抱怨.


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.


我想你的意思是这样的

I assume you mean something like this

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:

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

           }



如果您可以告诉我们更多有关您要实现的目标的信息,我们也许可以提供更多帮助.

为了回答您的评论,我将其更改为使用If构造,如下所示:



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:

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


这就是switch条件在C#中的工作方式.它期望case的恒定值.

如果您将变量声明为constant,如下面的代码所示,那么它不会给您错误,但是对于硬编码的值不会产生任何区别.

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.

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;
}


这篇关于我如何使用随身携带的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆