Accessing Case value in Case statement.

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
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.
 
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

Code:
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
Code:
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
 
Last 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

Code:
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?
 
Like this

value always contains the value you are looking for.

Code:
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
 
Like this

Code:
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.
 
Back
Top