Use Select Case instead of If-ElseIf

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

EnvisionCAD

Since 1996, EnvisionCAD has been a nationally recognized leader in the configuration, customization, implementation, training & support for CAD software solutions. Our individualized approach has benefited private engineering firms and government agencies alike. Basic or advanced, we can help you get the most from your CAD technology.

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *