Using Or operator in Select Case Statement in Visual Basic

I'm learning to use the Select Case statement in vb.

Select...Case Statement
https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/select-case-statement


I have a ASP.NET WebForms page using VB.

The example works fine.

vbsample.PNG
When I enter a name from one of the names in the button click event.
The message is displayed on the label.


This is my code.

Example1_SelectCase.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Example1_SelectCase.aspx.vb" Inherits="Example1_SelectCase" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td><asp:Label ID="lblCaption" runat="server" Text="Enter FirstName:"></asp:Label></td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
            <td></td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

Open in new window



Example1_SelectCase.aspx.vb

Partial Class Example1_SelectCase
    Inherits System.Web.UI.Page

    ' Select Case Statement
    'https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/select-case-statement

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim varFirstName As String

        ' get textbox value and store in variable
        varFirstName = TextBox1.Text

        Select Case varFirstName
            Case "Bob"
                Label1.Text = "Hi Bob"
            Case "Beth"
                Label1.Text = "Hi Beth"
            Case "John"
                Label1.Text = "Hi John"
            Case "Billy", "Willy"
                Label1.Text = "Hi Billy or Willy"
            Case "Joe"
                Label1.Text = "Hi Joe"
            Case "Susan"
                Label1.Text = "Hi Susan"
        End Select

    End Sub
End Class

Open in new window



In one of the case statements I have this:

            Case "Billy", "Willy"
                Label1.Text = "Hi Billy or Willy"

In this article
https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/select-case-statement

It states the following:

A Case statement with multiple clauses can exhibit behavior known as short-circuiting. Visual Basic evaluates the clauses from left to right, and if one produces a match with testexpression, the remaining clauses are not evaluated. Short-circuiting can improve performance, but it can produce unexpected results if you are expecting every expression in expressionlist to be evaluated.

Is it better to use an Or operator?
How do I revise this case statement to use the Or operator instead?

            Case "Billy", "Willy"
                Label1.Text = "Hi Billy or Willy"

So if I type either Billy OR Willy then display "Hi Billy or Willy" on the label.
LVL 1
maqskywalkerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
in that very specific case, you don't need to replace the short-circuiting with a regular Or because you are comparing against constant value (Billy or Willy). You are not running an expression. I wrote an article 10 years ago that explains what is short-circuiting: https://www.emoreau.com/Entries/Articles/2008/04/Short-circuiting-mainly-in-VBNet-and-SQL-Server.aspx

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.