试图在C#中使用switch语句

时间:2013-06-06 15:40:10

标签: c#

我正在检查文本框控件“txtType”中的值,我有3个可能的值:它是TypeA,TypeB或TypeC。所以我想做的就是: 这就是我到目前为止所拥有的

string myType= ((TextBox)DV_LogAdd.FindControl("txtType")).Text.ToString();
int  updatedType;
If string myType = ‘TypeA’ then set updatedType to 1
If string myType = ‘TypeB’ then set updatedType to 2
If string myType = ‘TypeC’ then set updatedType to 3

我试过使用switch语句但搞砸了。

9 个答案:

答案 0 :(得分:5)

试试这个:

        string myType= ((TextBox)DV_LogAdd.FindControl("txtType")).Text.ToString();
        int updateType = 0;
        switch (myType)
        {
            case "TypeA":
                updateType = 1;
                break;
            case "TypeB":
                updateType = 2;
                break;
            case "TypeC":
                updateType = 3;
                break;
            default :
                throw new ArgumentException("txtType value not supported :"+myType);
        }

答案 1 :(得分:2)

你试过了吗?

switch(myType)
{
    case "TypeA":
        updatedType = 1;
    break;
    case "TypeB":
        updatedType = 2;
    break;
    case "TypeC":
        updatedType = 3;
    break;
    default:
    break;
}

答案 2 :(得分:2)

string myType= ((TextBox)DV_LogAdd.FindControl("txtType")).Text.ToString();
int updatedType;
switch(myType) 
{ 
   case "TypeA"
      updatedType = 1;
      break;
   case "TypeB":
      updatedType = 2;
      break;
   case "TypeC"
      updatedType = 3;
      break;
   default:
      updatedType= 0;
}

答案 3 :(得分:1)

这里switch语句在http://msdn.microsoft.com/library/06tc147t(v=vs.80).aspxhttp://msdn.microsoft.com/en-US/library/k0t5wee3(v=vs.80).aspx

完整解释

使用开关,您的代码将如下所示:

string myType= ((TextBox)DV_LogAdd.FindControl("txtType")).Text.ToString();
int updateType = 0;
switch (myType)
{
    case "TypeA": 
        updateType = 1;
        break;
    case "TypeB":
        updateType = 2;
        break;
    case "TypeC":
        updateType = 3;
        break;
    default:
        // Do some stuff
        break;
}

答案 4 :(得分:1)

希望这会对你有所帮助

        string myType= ((TextBox)DV_LogAdd.FindControl("txtType")).Text.ToString();
        int updatedType;
        switch (myType)
        {
            case "TypeA": updatedType = 1;
                break;
            case "TypeB": updatedType = 2;
                break;
            case "TypeC": updatedType = 3;
                break;
            default: updatedType = 0; //Optionnal if myType not in (TypeA, TypeB, TypeC); otherwise, you muste initialize updatedType
                break;
        }

答案 5 :(得分:1)

你的switch语句看起来不像C#而只是psuedo代码,但C#等价物是:

switch(myType){
 case "TypeA":
  updateType=1;
  break;
 case "TypeB":
  updateType=2;
  break;
 case "TypeC":
  updateType=3;
  break;
 default:
  break;
 }

答案 6 :(得分:1)

这对我有用:

string myType  = "typeB";

int updatedType  = 0;
switch(myType) {
    case "typeA":
        updatedType = 1;
        break;
    case "typeB":
        updatedType = 2;
        break;
    case "typeC":
        updatedType = 3;
        break;
}

Console.WriteLine("number: " + updatedType .ToString());

答案 7 :(得分:1)

如果我找对你,我想你想要的是......

string str = "";
int  updatedType;

foreach(Control c in this.controls)
{
   if(c.GetType() == typeof(TextBox) && c.Name == "txtType")
   {
      str = c.Text;
   }
}

switch(str)
{
   case "TypeA":
        updatedType = 1;
        break;
   case "TypeB":
        updatedType = 2;
        break;
   case "TypeC":
        updatedType = 3;
        break;
   default:
        break;
}

答案 8 :(得分:0)

string[] data = {"", "TypeA", "TypeB", "TypeC"};

string myType = ((TextBox)DV_LogAdd.FindControl("txtType")).Text;
int updatedType = Array.IndexOf(data, myType);

注意:如果找不到数组中的项目,它将返回-1