Switch Function in VB.NET
Submitted by donbermoy on Thursday, April 10, 2014 - 14:37.
In this tutorial, we will create a program that can get the name of the country by inputting the capital city using the switch function.
Now, let's start this tutorial!
1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.
2. Next, add only one Button named Button1 and labeled it as "Answer". Insert two texboxes named TextBox1 for inputting the capital city and TextBox2 for displaying the equivalent country of the input. You must design your interface like this:
3. Create a function named matchCountry as String that has the parameter of a string variable named capitalCity.
We have created a function named matchCountry that is a string to provide the equivalent city of their capitals. We have use the return statement to specify the return value of the property or function in our matchCountry. The CStr function here forces the return value to have a string out of any variants. Microsoft.VisualBasic.Switch switches the first string held by the capitalCity variable and will switch with the second string of it. This return the textbox2 value corresponding to the first expression in the list.
4. Put this code in your Button1_Click. This will trigger to get the equivalent city out of the inputted capital city of textbox1 that will be displayed in textbox2.
Download the source code below and try it! :)
For more inquiries just contact my number or e-mail below.
Best Regards,

- Function matchCountry(ByVal capitalCity As String) As String
- Return CStr(Microsoft.VisualBasic.Switch( _
- capitalCity = "Manila", "Philippines", _
- capitalCity = "Canberra", "Italian", _
- capitalCity = "New Delhi", "India", _
- capitalCity = "Doha", "Qatar", _
- capitalCity = "Taipei", "Taiwan"))
- End Function
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- TextBox2.Text = matchCountry(TextBox1.Text)
- End Sub
Output:

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]
Visit and like my page on Facebook at: Bermz ISware Solutions
Subscribe at my YouTube Channel at: SerBermz
Add new comment
- 415 views