Search Tools Links Login

Select Case


Visual Basic 6, or VB Classic

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

Code

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

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 :


A:

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.


B:

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
1


' Do Something

Case 2 ' Text1.Text Equaled
2


' 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
much

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
it

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
If
...... routine.

About this post

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

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.