c# - 尝试在 C# 中使用 switch 语句

标签 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 语句但搞砸了。

最佳答案

试试这个:

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

关于c# - 尝试在 C# 中使用 switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16966393/

相关文章:

c# - 为什么这两个函数不返回相同的值?

c# - WPF 如何将 XML 与多个命名空间绑定(bind)

c# - 此客户端的消息提交速率已超过配置的限制?

c# - 使用 Entity Framework 4.0 更新分离的实体实例

c# - XMLSerializer 序列化日期错误 : Is not a valid AllXsd

c# - 删除所有一对多关联项 - Telerik OpenAccess ORM

c# - 我可以在 DataValueField 中有多个值吗?

c# - ASP.net Web 应用程序(非 MVC): Accessing Google GoogleWebAuthorizationBroker. AuthorizeAsync 返回拒绝访问错误

c# - 总计 Gridview 上的列并显示在页脚中

c# - 不使用乘法C#将字符串转换为整数