Search Tools Links Login

Select Case

A beginner's look into the Select Case Structure .
Comparing Select Case to If...ElseIf....End If....
This Tutorial is for beginner's .

Original Author: Danny Pryor


Comparing Select Case
to If...ElseIf....End

Whenever possible try to use the Select Case structure.

Reason's are as follow's ;

A> Better understandability
of the code you just wrote.

B> Lead's to smaller
coding structure's .

C> Will decrease
the Informational Complexity of the

procedure by at least 10% ( This is a good thing ! )

D> Lead's to re-use-ability
of your code .

For instance ;

Look at the following structure's :


Private Sub DecisionStructuresCollide()

If Text1.Text = 1 Then

' Do something

ElseIf Text1.Text = 2 Then

' Do something

End If

End Sub

To use the above structure is messy and will

lead to relentlessly

repeated code , thus over-bloating your procedure.


Private Sub DecisionStructuresCollide()

Dim sMyString As String
' Build the Variable Structure

sMyString = Text1.Text ' Pass the Text in Text1.Text into
the Variable as a String

Select Case sMyString

Case 1 ' Text1.Text Equaled

' Do Something

Case 2 ' Text1.Text Equaled

' Do Something else

Case Else ' Text1.Text Equaled
something else so

' we can still be flexible and do

' something else. Although the Case

' Else statement is not required

SeeInCaseProcedureDoSomethingElse ' Do Something else

End Select ' Each Select
Case Statement must be

' accompanied with a closing End Select

' statement

End Sub

Example B is definately much cleaner code and

easier to read , not to mention the benefit's from the

Informational Complexity being smaller.

I'm not saying that in 100% of all cases should one use the

Select Case Structure , however , whenever possible

will lead to a much better coding practice that you would

automatically try to stick to and then if your procedure

cannot be done by using this structure , then you can fall

back onto the old If.....ElseIf......End
...... routine.

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 93 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.