Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Using Or operator in Select Case Statement in Visual Basic

Avatar of maqskywalker
maqskywalker asked on
ASP.NETVisual Basic.NET
1 Comment1 Solution99 ViewsLast Modified:
I'm learning to use the Select Case statement in vb.

Select...Case Statement

I have a ASP.NET WebForms page using VB.

The example works fine.

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.


<%@ 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">
    <form id="form1" runat="server">
            <td><asp:Label ID="lblCaption" runat="server" Text="Enter FirstName:"></asp:Label></td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="Button" />
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>


Partial Class Example1_SelectCase
    Inherits System.Web.UI.Page

    ' 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

In one of the case statements I have this:

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

In this article

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.
Avatar of Éric Moreau
Éric MoreauFlag of Canada imageSenior .Net Consultant
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answers