Use Select Case instead of If-ElseIf

Published on May 6, 2009

When performing multiple comparisons it is easy to get trapped into using multiple If-ElseIf statements. While that will certainly accomplish the task a cleaner, and more efficient, way to do this is to use the Select Case construct. Examples of the same comparison are shown below. In the Select Case example notice that you are not limited to comparing a single variable.

If-ElseIf

If iStart < 0 Then
    ‘Do something here
ElseIf iStart > iEnd Then
    ‘Do something here
ElseIf iCount = iStart Then
    ‘Do something here
ElseIf iCount = iEnd Then
    ‘Do something here
Else
    ‘Do something here
End If

Select Case

Select Case True
Case (iStart < 0)
	‘Do something here
Case (iStart > iEnd)
	‘Do something here
Case (iCount = iStart)
	‘Do something here
Case (iCount = iEnd)
	‘Do something here
Case Else
	‘Do something here
End Select

Tags: ,
Like this article? Share it!

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Privacy Policy