Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

In a Case statement is there a way to access the value of the Case?

 

Given that you can say " Case 4 To 10" for instance, this would be a useful thing to do.

 

Thanks.

Posted (edited)

You have the value from your case evaluation that should tell you all you need to know.

 

I am not a VB programmer, so you'll have to do with a C# example

 

switch (value)
{
    case 1:
    case 2:
    case 3:
        // value could be 1, 2 or 3
        break;
    case 4:
    case 5:
    case 6:
        // value could be 4, 5 or 6
        break;
    default:
        // value can be anything else
        break;
}

 

Here it in in VB

Select Case value
Case 1, 2, 3
	' value could be 1, 2 or 3
	Exit Select
Case 4, 5, 6
	' value could be 4, 5 or 6
	Exit Select
Case Else
               ' value can be anything else
	Exit Select
End Select

Edited by Napivo1972
Posted
You have the value from your case evaluation that should tell you all you need to know.

 

I am not a VB programmer, so you'll have to do with a C# example

 

switch (value)
{
    case 1:
    case 2:
    case 3:
        // value could be 1, 2 or 3
        break;
    case 4:
    case 5:
    case 6:
        // value could be 4, 5 or 6
        break;
    default:
        // value can be anything else
        break;
}

 

You leave me none the wiser unfortunately. If, as you say, the value could be 1,2 or 3 at this point "// value could be 1, 2 or 3" how do you know which it is?

Posted

Like this

 

value always contains the value you are looking for.

 

Select Case value
Case 1, 2, 3

	If value = 1 Then
	End If


	If value = 2 Then
	End If


	If value = 3 Then
	End If

	' value could be 1, 2 or 3
	Exit Select
Case 4, 5, 6
	Select Case value
		Case 4
                             ' value is 4
                       Case 5
                             ' value is 5
                       Case 6
                             ' value is 6
	End Select
	' value could be 4, 5 or 6
	Exit Select
Case Else

	Exit Select
End Select

Posted
Like this

 

Select Case value
Case 1, 2, 3

	If value = 1 Then
	End If


	If value = 2 Then
	End If


	If value = 3 Then
	End If

	' value could be 1, 2 or 3
	Exit Select
Case 4, 5, 6
	' value could be 4, 5 or 6
	Exit Select
Case Else

	Exit Select
End Select

 

Er, yeah, I suppose so, having a bit of a moment. I was hoping there might be some syntax like "If Case =5 Then" but sure, it's not really necessary.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...