Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
Sign in to follow this  
formlesstree4

Lesson 4 - Select Case Statements

Recommended Posts

This one ties very closely with Lesson 3 in how to logically process an item. Basically, a Select Case statement is multiple If-Then-Else (ITE) statements.

There are several benefits to using a Select Case:

1. When you have an item with multiple possibilities and want to check all of them.

2. Code cleanliness

3. Readability.

4. Prevents nestled ITE errors.

 

There are more, but it's late and I don't know of anymore right now :)

 

Now, let's say I have an Integer called "x"

Dim x As Integer

And lets say I plug it into a function, randomNumber()

x = randomNumber()
Private Function randomNumber() As Integer
    'Do random stuff here'
End Function

 

Just for simplicities sake, let's say the randomNumber will only generate a number between 1 and 4. You want to do something different at each number.

 

Using ITE's, that can look really nasty:

If x = 1 then
  'do this
ElseIf x = 2 then
  'do this
ElseIf x = 3 then
  'do this
ElseIf x = 4 then
  'do this
End If

 

If you're not careful, typing all those ElseIf's and thens, then back to elseif's, it just looks nasty after a time. What if you have to nestle if statements? The readability is much harder.

A Case statement changes all that to this:

Select Case x
Case 1
  'Do stuff
Case 2
  'Do stuff
Case 3
  'Do stuff
Case 4
  'Do stuff
Case Else
  'Show error
End Select

You can see how much easier that is to read. The program will do something at the specified line when x matches one of those cases.

If you see, I added an extra line: "Case Else". This is if the variable "x" doesn't match any of the other cases (aka, x <> 1, 2, 3, or 4). In any controlled environment, you will probably never need to use the Case Else, but there's always that one time where x may hit 0, or 5, or any other number. The Case Else will handle that entirely, allowing you to take appropriate action and prevent your program from erroring!

 

I talked earlier about nestled ITE's and how they can look nasty. Well, here's an example of one [simple] nestled ITE:

If x > 3
  If x < 8 Then
     'Do this
  elseIf x <5 Then
     'do this
  End if
End If

If you look carefully, doing a Select Case actually increases the line count.

Select Case x
  Case x > 3
     If x < 8 Then
        'Do this
     ElseIf x < 5 Then
        'Do this
     End If
End Select

 

That is a rather poor example, but you can see how using a Select Case is not always the answer.

 

If you have any questions, please let me know!

Edited by formlesstree4
I need sleep..completely blew the last section!

Share this post


Link to post
Share on other sites

I think you're going to have to supply and example for me on this one. :unsure:

 

 

^^Says the VB nooby.

Share this post


Link to post
Share on other sites

Select Case FileInformation(0)
               Case "bitmapBatch"
                   mode = FileTypes.Bitmap
               Case "StructureBatch"
                   mode = FileTypes.Struct
               Case "LightmapsBatch"
                   mode = FileTypes.Lights
               Case "CacheBatch"
                   mode = FileTypes.BuildM
           End Select

Chooses the information based on the contents of the FileInformation(0), which is a string from a Zero-Based Integer Array.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...