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?
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.
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.
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.
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).
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 if
s to get the required action.
Reason? C#'s switch
requires constants for the various case
s. This is different from VB.NET's Select Case
which allows writing ranges.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With