A casing technique in Visual Basic coding

One of famous common easy techniques in Visual Basic casing structures which is familiar to most of Visual Basic developers is the ability to compare a value with several variables in Select Case statements.

It has been pointed many times but as I saw the lack of this technique in someone’s code write about it again here.

The base point is “You can compare a constant value with several variables instead of comparing a variable with several values”.  Take a look at following code in a Windows Console application.  It simply gets three string values and wants to compare each variable with a constant string value.  If it could find any equivalent string, will show a MessageBox with the name of the variable contains that string.  Probably it’s not a real world sample but can show my purpose.

Sub Main()

    Dim String1, String2, String3 As String

    Console.WriteLine("Enter String1:")

    String1 = Console.ReadLine()

    Console.WriteLine("Enter String2:")

    String2 = Console.ReadLine()

    Console.WriteLine("Enter String3:")

    String3 = Console.ReadLine()

 

    If (String1 = "That's me!") Then

        MsgBox("String1")

    End If

    If (String2 = "That's me!") Then

        MsgBox("String2")

    End If

    If (String3 = "That's me!") Then

        MsgBox("String3")

    End If

End Sub

For three variables, I used three If cases to check their values.  for three variables it seems to have no problem but if you want to check more variables your code won’t be logical, won’t have good structure and will have many lines.  By using this technique you can replace above code with below code which is simpler and meaningful and for more variables will have shorter lines.

Sub Main()

    Dim String1, String2, String3 As String

    Console.WriteLine("Enter String1:")

    String1 = Console.ReadLine()

    Console.WriteLine("Enter String2:")

    String2 = Console.ReadLine()

    Console.WriteLine("Enter String3:")

    String3 = Console.ReadLine()

 

    Select Case "That's me!"

        Case String1

            MsgBox("String1")

        Case String2

            MsgBox("String2")

        Case String3

            MsgBox("String3")

    End Select

End Sub

That’s it!  This is one of famous Visual Basic coding techniques but still there are many developers who miss it.  Try it in C# to know why it’s sharp!

Now playing: Pink Floyd - Mother

[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.

13 Comments : 07.16.06

Feedbacks

 avatar
#1
J-O Eriksson
07.16.2006 @ 1:47 PM
Aahh, I see you have looked at my code ;-) I know, I always do sloppy things in the beginning to get things working, then go through again and try to clarify. But if it was mine you looked at it was more like. If (String1 <> "") Then Me._String1 = String1 End If If (String2 <> "") Then Me._String1 = String2 End If If (String3 <> "") Then Me._String1 = String3 End If Which also of course gets unreadable after while.
admin avatar
#2
Keyvan Nayyeri
07.16.2006 @ 7:28 PM
Case logical statement doesn't matter J-O, You can put any logical statement in your cases :-)
 avatar
#3
J-O Eriksson
07.17.2006 @ 5:23 AM
But I want a unlogical statement ;-) No, but like I said, I am always very lazy in the beginning writing code, and when I see that it works I try to clean it up. But your article made me see how I can do it.
admin avatar
#4
Keyvan Nayyeri
07.17.2006 @ 9:13 AM
Being lazy or not isn't the problem. You're spending your time to write codes so it's better to write a logical code instead of unlogical code because second one will waste your time later yo debug and/or structure it. Although probably there are some developers who follow your approach but it's not a recommended approach at all. Following this way in large scale systems will cause to many problems.
 avatar
#5
Nick
07.17.2006 @ 1:02 PM
If I remember rightly from the Computing A-Levels CASE statements are alot parsed quicker than large IF statements
 avatar
#6
J-O Eriksson
07.17.2006 @ 2:21 PM
Just realized I cannot use a case statement in the part of my code that I was talking about. Not without changing some of the other code. In my case I need the code to go through each case, and not stop when it found a case that is true. If it found that String1 <> "" then I need it to continue and also check if String2 <> "" etc. To use a SELECT CASE I need to know how many loops it should have. In current code I don't know that. But I'll find a way.
 avatar
#7
J-O Eriksson
07.17.2006 @ 2:23 PM
Come to think of it, a loop in my case would not solve it either. Even if I loop for example 5 times, everytime it will exit the SELECT CASE at the first CASE statement.
admin avatar
#8
Keyvan Nayyeri
07.17.2006 @ 8:14 PM
J-O, What I see from the code you've provided in your first comment seems bit different from what you say. You're assigning some values just to one variable so if your code walks through all cases finally _String1 just has one value.
admin avatar
#9
Keyvan Nayyeri
07.17.2006 @ 8:50 PM
Nick, you're absolutely right :-)
 avatar
#10
Jayson Knight
07.18.2006 @ 12:53 AM
Sounds like a good "case" for recursion IMO ;-).
admin avatar
#11
Keyvan Nayyeri
07.18.2006 @ 2:45 AM
Yeah :-P
 avatar
#12
J-O Eriksson
07.20.2006 @ 11:54 AM
Yes I see now that my first comment was a bit wrong, to much copy/paste :-). The code in my fist comment should've been: If (String1 <> "") Then Me._String1 = String1 End If If (String2 <> "") Then Me._String2 = String2 End If If (String3 <> "") Then Me._String3 = String3 End If Note the changed variables within each if section.
admin avatar
#13
Keyvan Nayyeri
07.20.2006 @ 1:11 PM
Man, do you change your code to beat me?! :-P Again, you can use this technique for this new code as well :-D NP, I just wanted to point to this technique because it's a hidden one from some developers eyes ;-)

Leave a Comment