Switch Function in VB.NET

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: design 3. Create a function named matchCountry as String that has the parameter of a string variable named capitalCity.
  1. Function matchCountry(ByVal capitalCity As String) As String
  2. Return CStr(Microsoft.VisualBasic.Switch( _
  3. capitalCity = "Manila", "Philippines", _
  4. capitalCity = "Canberra", "Italian", _
  5. capitalCity = "New Delhi", "India", _
  6. capitalCity = "Doha", "Qatar", _
  7. capitalCity = "Taipei", "Taiwan"))
  8. End Function
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.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. TextBox2.Text = matchCountry(TextBox1.Text)
  3. End Sub

Output:

output Download the source code below and try it! :) For more inquiries just contact my number or e-mail below. Best Regards,

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