Select Case
Posted: 2003-06-01
By: ArchiveBot
Viewed: 61
Filed Under:
No attachments for this post
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
Whenever possible try to use the Select Case structure. For instance ; A: To use the above structure is messy and will B: Select Case sMyString SeeInCaseProcedureDoSomethingElse ' Do Something else End Select ' Each Select End Sub Example B is definately much cleaner code and I'm not saying that in 100% of all cases should one use theComparing Select Case
to If...ElseIf....End
If....
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 .
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
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
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
Case Statement must be
' accompanied with a closing End Select
' statement
much
easier to read , not to mention the benefit's from the
Informational Complexity being smaller.
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.
Comments on this post
No comments have been added for this post.
You must be logged in to make a comment.