Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Select...Case Statement Equivalent in C#

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?

like image 430
Alex Essilfie Avatar asked Mar 15 '10 12:03

Alex Essilfie


People also ask

Is select case a conditional statement?

Select Case is a conditional statement, that helps you test a variable for equality against a set of values. Each value is referred to as a case, and a variable that is being switched on should be checked for all the select cases.

How do you write switch statement in VB?

To execute a group of statements depending upon the value of an Expression, then we use the Switch Case. Here, each value is called a Case, and the variable is being switched ON based on each case. Else statement case is executed if the test expression doesn't match with any of the Case specified by the user.

What is Select Case statement in computer?

A SELECT CASE statement, often referred to as a CASE statement, is used to compare a given value with preselected constants and take an action according to the first constant to match. A CASE statement can be handy to select between a series of different possibilities or cases.

Can you use a Select Case statement to test different values of strings?

Select Case may be used on a String. With this statement we match a variable against a set of values such as String literals. But On Strings, Select Case offers no performance advantage as with Integers (or other values).


1 Answers

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.

like image 111
Alex Essilfie Avatar answered Sep 22 '22 21:09

Alex Essilfie