Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Golden West Colleges

College of Information Technology Education


Alaminos City, Pangasinan

CC103
Intermediate Programming

Prepared by:
Kairos Jil I. Molina
molinakairos@gmail.com
Lesson
Select Case
8 Statement
What is Select Case in VB.Net?
 Select Case is a conditional statement, that helps you test a variable for equality against
a set of values. Each value is referred to as a case, and a variable that is being switched
on should be checked for all the select cases.
 The Select Case statement provides you with an easy way of testing for the contents of
a variable. However, it is only suitable for use when a variable in question has only a
limited number of options.

Select Case Syntax

Select [ Case ] your_expression


[ Case expression_list
[ statement(s) ] ]
[ Case Else
[ else statement(s) ] ]
End Select

 your_expression: this denotes an expression which evaluates to one of the


elementary Data Types supported in Microsoft VB.NET.
 expression_list: expression clauses that denote the match values for the expression.
For the case of multiple clauses, separate them using a comma (,).
 statement(s): statements that follow the Case and they execute after the select
expression has matched any clause in expression_list.
 else statements: statements that follow the Case Else and run once the select
expression fails to match any of the clauses in the expression_list for any Case
statement.

Sample Code:

Dim name As String


name = "Jose Rizal"

Select Case name


Case "Juan Luna"
MessageBox.Show("Hello Juan Luna")
Case "Andres Bonifacio"
MessageBox.Show("Hello Andres Bonifacio")
Case "Emilio Aguinaldo"
MessageBox.Show("Hello Emilio Aguinaldo")
Case "Jose Rizal"
MessageBox.Show("Hello Jose Rizal")
Case Else
MessageBox.Show("Hello World")
End Select

Output:
Sample Code:

Dim name As String


name = txtName.Text

Select Case name


Case "Rizal"
MessageBox.Show("Hello Rizal")
Case "Juan"
MessageBox.Show("Hello Juan")
Case "Crisostomo"
MessageBox.Show("Hello Crisostomo")
Case "Emilio"
MessageBox.Show("Hello Emilio")
Case Else
MessageBox.Show("Hello World")
End Select
MessageBox.Show("VB.NET is easy!")

ToLower() and ToUpper() Functions


 The Select Case statement is case sensitive. This means that it will treat rizal as
different from Rizal. However, we can use the ToLower() and the ToUpper() functions to
handle the issue of a case with this statement.

Sample Code:

Dim name As String


name = txtName.Text

Select Case name.ToLower


Case "rizal"
MessageBox.Show("Hello Rizal")
Case "juan"
MessageBox.Show("Hello Juan")
Case "crisostomo"
MessageBox.Show("Hello Crisostomo")
Case "emilio"
MessageBox.Show("Hello Emilio")
Case Else
MessageBox.Show("Hello World")
End Select
MessageBox.Show("VB.NET is easy!")

The value of the variable name will be used for performing comparisons with the various Case
statements to find a match. The ToLower() function will ensure that any name the user types is
first converted into lowercase before the evaluation of Case statements. This means that if the
user types Rizal, it will be immediately converted to rizal, then the evaluation of the Case
statements is done.

Summary
 The Select Case statement provided by VB.NET helps you evaluate a value against a
set of values through matching.
 It is only suitable if the possible values of the variable in question is known to be limited.
 The Select Case statement is case sensitive.
 You can use the ToLower() and the ToUpper() functions to convert strings into
lowercase and uppercase respectively.

Reference:
https://www.guru99.com/vb-net-select-case.html
Lesson
Exception Handling
9 in VB.Net
What is an Exception in VB.Net?
 An Exception in VB.Net refers to a problem that arises during program execution. It is
brought about by an unexpected circumstance. A good example is when you are
performing a division operation, and then you divide by zero (0). An exception will be
raised.

What is Exception Handling in VB.Net?


 With Exceptions in VB.Net, you can transfer the control of a program from one part to

another. In VB.Net, exceptions are handled using the following 4 keywords:

Try Catch Syntax


Try
[ try_Statement(s) ]
[ Exit Try ]
[ Catch [ exception_name [ As type ] ] [ When expression ]
[ catch_Statement(s) ]
[ Exit Try ] ]
[ Catch ... ]
[ Finally
[ finally_Statement(s) ] ]
End Try

 The Try/Catch block should surround the code that may raise an exception. This code is
known as a protected code. You can use multiple catch statements when you need to
catch various types of exceptions.

Sample Code:

Dim answer As Integer


Dim n1 As Integer = 4
Dim n2 As Integer = 0
Try
answer = n1 \ n2
Catch ex As DivideByZeroException
MessageBox.Show("Exception: " & ex.Message)
Finally
MessageBox.Show("Answer is: " & answer)
End Try
Output:

User Defined Exceptions in VB.Net


 VB.Net allows you to define your own exceptions. You can get user-defined exception
classes from ApplicationException class.

https://www.guru99.com/try-catch-finally-vb-net.html

You might also like