Keyvan Nayyeri

God breathing through me

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

13 Comments

J-O Eriksson
Jul 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.

Keyvan Nayyeri
Jul 16, 2006 7:28 PM
#
Case logical statement doesn't matter J-O, You can put any logical statement in your cases :-)

J-O Eriksson
Jul 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.

Keyvan Nayyeri
Jul 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.

Nick
Jul 17, 2006 1:02 PM
#
If I remember rightly from the Computing A-Levels CASE statements are alot parsed quicker than large IF statements

J-O Eriksson
Jul 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.

J-O Eriksson
Jul 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.

Keyvan Nayyeri
Jul 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.

Keyvan Nayyeri
Jul 17, 2006 8:50 PM
#
Nick, you're absolutely right :-)

Jayson Knight
Jul 18, 2006 12:53 AM
#
Sounds like a good "case" for recursion IMO ;-).

Keyvan Nayyeri
Jul 18, 2006 2:45 AM
#
Yeah :-P

J-O Eriksson
Jul 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.

Keyvan Nayyeri
Jul 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





Ads Powered by Lake Quincy Media Network