Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.
VB.NET from beginner to advanced programmer
- Introduction and Installation
- Objects and Events
- Variables
- The basic data types
- Logical Operators
- Relational Operators
- If statements Then
- Arithmetical Operators
- Loops Part 1
- Arrays
- Loops Part 2
- Try Catch statements
- Subs and Functions
- Difference between Scopes
- Select Statements
- Multidimensional arrays
- Structures
- Classes
- Enumerations
- Advanced Comments
- Compiling Directives
Remember I talked about an option to if statemetns earlier? The option is called Select Case statements. The select case statement is good to use when you want different blocks of code to be executed depending on different values on one variable. If statements is good if you only need to test one value or if you have to test different variables(can't be done with select case).
Anyway, to use a select case statement you have to create it by writing "Select Case" and then the value you want to test. End the statement with "End Select". Could look something like this:
Code:Select Case "test" End SelectCode:Select Case myVariable End Select
At the moment our select case statements are empty, inside them we will add cases by writing "Case " and then the value for that case. Then the program will get the value of the select case statement(as we did above) and then check if that value is the same as the first case. If it is the associated block of code will be run or else it will try next until it founds one. You can also add an "Case Else" block at the end which will be executed if no "normal" case's condition has been true. A few examples:
Code:Select Case myVariable Case 1 MessageBox.Show("myVariable = 1") Case 2 MessageBox.Show("myVariable = 2") Case 3, 4 MessageBox.Show("myVariable = 3 or 4") Case 5 MessageBox.Show("myVariable = 5") Case Else MessageBox.Show("myVariable is nether 1,2,3,4 or 5") End SelectCode:Select Case "CodeCall" Case "test" MessageBox.Show("Testing...") Case "CodeCall" MessageBox.Show("CodeCall rules!") Case "hi" MessageBox.Show("Hello") End Select
Observe that the select case statement will always only run a maximum of one block even if more then one's condition is True. In the example below we will only see one messagebox with the message "This will be shown".
Code:Select Case 5 Case 321 MessageBox.Show("This is False") Case 5 MessageBox.Show("This is True and will be shown") Case 5 MessageBox.Show("This is True but won't be shown") End Select
More advanced Select Case Statements
To:
When using some sort of values(Integers for example) you can add a whole range of numbers to one case by using "[number] To [number]", like this:
Code:Select Case myVariable Case 0 To 5 MessageBox.Show("myVariable is between 0 and 5") Case 6 To 10 MessageBox.Show("myVariable is between 6 and 10") Case 11 MessageBox.Show("myVariable = 11") End Select
Is:
You can also add relational operators to your select case statement. Do this by replace "Case [value]" with "Case Is [relational operator] [value]". Actually a select case statement does always contains a relational operator, the is equals to operator (=) even though it's hidden. This means that "Case Is = myValue" is the same as "Case myValue". Here comes a little example on how to use it:
Code:Select Case myVariable Case Is > 0 MessageBox.Show("myVariable is greater then 0") Case Is < 0 MessageBox.Show("myVariable is less then 0") Case Else MessageBox.Show("myVariable is equals to 0") End Select
And that's what I had about Select Case Statements. If you don't know when to use them and when to use If Statements you can look here for some tips: If Statements VS Switch Statement - CodeCall Programming Wiki Note that that article is written generally for all Programming languages and therefor the Select Case statement is called Switch Statement. Hope I didn't lost you there. See you in the next part.![]()
Last edited by Vswe; 03-21-2010 at 03:35 PM.
Select statements are so much easier to read than If statements, so much cleaner IMO. I've read that they are faster as well but never benchmarked them. Nice work +rep!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks