在C#VB.NET选择... Case语句等效 [英] VB.NET Select...Case Statement Equivalent in C#

查看:429
本文介绍了在C#VB.NET选择... Case语句等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用C#和我有几个问题。
有什么办法来编码C#相当于像下面VB.NET Select语句?



<预类=朗动prettyprint-覆盖> 选择Object.Name.ToString()
案名1
'做一些
案名称2
'做点别的
案例否则
'执行默认操作
端选择

任何帮助将不胜感激






感谢您的输入到目前为止,现在是什么样,如果我勾几个控制一个事件处理程序,下面和我想执行的每一个控制一个稍微不同的动作:



<预类=朗动prettyprint-覆盖> 私人小组Button_Click(发件人为对象,E为EventArgs的)_
把手button1.Click,Button2.Click

'做一般活动

选择CTYPE(发件人,巴顿).Name点
案例button1.Name
'做些什么
案例button2.Name
'做点别的
案例否则
'做defalut动作
端选择
端子

是否有这样做在C#上面的select语句,而无需任何方式使用嵌套IFS?


解决方案

我来发现随着时间的推移,一些VB.NET 选择.. 。病例结构没有在C#应用和周围的唯一办法是写了很多IFS的。



例如,在VB.NET ,你可以写:



<预类=朗动prettyprint-覆盖> 暗淡NUM为整数= 5

选择案例NUM
案例1至10
'做些什么

案例11至20
'做的另一件事

案例否则
'做的默认
端选择

但没有开关构建在C#中,可以让你做这种东西。你必须在迂回的代码如下所示:

  INT NUM = 5; 

开关(NUM)
{
案例1:
案例2:
案例3:
案例4:
案5:
案例6:
案例7:
案例8:
案例9:
案例10:
//做些什么
断点;
壳体11:
壳体12:
壳体13:
壳体14:
壳体15:
壳体16:
外壳17:
案例18:
案例19:
案例20:
//做别的事情
中断;
默认:
//做默认的
中断;
}



或者,如果你碰巧使用是工作双或者是由连续的而不是离散值的类型,你将不得不使用如果 s到获得所需的操作。



原因是什么? C#的开关要求对各种情况取值常数。这是VB.NET的选择案例允许写范围不同。


I just started using C# and I've got a couple of issues. Is there any way to code the C# equivalent of the VB.NET Select statement like the following?

Select Object.Name.ToString()
    Case "Name1"
        'Do something
    Case "Name2"
        'Do something else
    Case Else
        'Do the default action
End Select

Any help would be greatly appreciated.


Thanks for the input so far now what about if I hook several controls to one event handler as in the following and I want to perform a slightly different action for each control:

Private Sub Button_Click(sender as Object, e as EventArgs) _
  Handles button1.Click, Button2.Click

    'do a general activity

    Select CType(sender, Button).Name
        Case button1.Name
            'do something
        Case button2.Name
            'do something else
        Case Else
            'do the defalut action
    End Select
End Sub

Is there any way of doing the above select statement in C# without having to use nested ifs?

解决方案

I have come to find over time that some VB.NET Select...Case constructs do not apply in C# and the only way around is to write a lot of ifs.

For instance, in VB.NET, you can write:

Dim num as Integer = 5

Select Case num
    Case 1 to 10
        'do something

    Case 11 to 20
        'do another thing

    Case Else
        'do the default
End Select

But there is no switch construct in C# that allows you to do something of this sort. You'll have to code in roundabout like so:

int num = 5;

switch (num)
{
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
        //do something
        break;
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
        //do something else
        break;
    default:
        //do the default
        break;
}

Or if you happen to be working with Double or any type which is made up of continuous instead of discrete values, you'll have to use ifs to get the required action.

Reason? C#'s switch requires constants for the various cases. This is different from VB.NET's Select Case which allows writing ranges.

这篇关于在C#VB.NET选择... Case语句等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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