Group,

I've got a complex If/Then statement that may require the use of "Select Case". However I'm not familiar with how "Select Case" actually works. Needless to say, I'm not sure if you can use it within an "If/Then" statement. Here's what I need to do:

If row > 7 And IsNumeric(Microsoft.VisualBasic.Left(txtLine, 2))_ 
And (Microsoft.VisualBasic.Mid(txtLine, 18, 1)).Contains("G", "T", "C", "W") Then_
    Go Do something constructive
Else 
    Go do something else
End If.

To explain, I'm reading some specific characters from a line in a text file. In the second part, I'm reading one (1) character and need to determine if it is a "G, T, C or W". I suspect that "Select Case" may be the way to do this. Needless to say the above code is incorrect. Would there be a proper way to write this.

FYI....

This line will actually be an "ElseIf" line as it is part of a bigger If/Then statement.

In advance, thanks for your help.

Don

Recommended Answers

All 4 Replies

A SELECT CASE would look something like:

Select Case Mid(txtLine, 18, 1)
    Case "G", "T", "C", "W"
        'Do this stuff only for those
    Case Else
        'Do some stuff
End Select

Here is an article that might help you get a little better understanding.

Visual Basic has two types of decision structures: If-Then and Select Case. If-Then is used when you have a general condition or multiple conditions to test to determine if code should be executed. The Select statement is for comparing a variable or calculation against a series of values, without recalculating or getting the variable's value again.

To include all the conditions you defined in the original, you should nest the structures. Also, I would use the String method Substr rather than the VisualBasic function Left or Mid.

If row > 7 And IsNumeric(txtLine.Substr(0, 2)) Then
    Select Case txtLine.Substr(17, 1)
    Case "G", "T", "C", "W"
        Rem Do something good.
    Case Else
        Rem Do something else.
    End Select
End If

Ahhhhh.... (and now the lightbulb comes on)! Excellent! Thanks for the advice and instruction. This is valuable! Thank you both!

A not so often used, but sometimes useful form of the Select Case is that it can be used to select between seemingly unrelated conditions. The form

Select Case True
    Case <condition>
    Case <condition>
    Case <condition>
    Case Else
End Select

will execute the first clause where <condition> evaluates to the expression in the Select Case statement. Because the Select Case clause always evaluates to True, the first Case that evaluates to True will be executed. You can see that the Case statements need not be related so that you could end up with clauses like

Select Case True
    Case x = 5
    Case y = 19
    Case IsEven(3*x + 2*y + 5)
    Case LastName = "Smith"
End Select

As you can see by my example, it can be used to produce horrible logic but used properly it can make your code cleaner.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.