.:: Jasa Membuat Aplikasi Website,Desktop,Android Order Now..!! | | Order Now..!! Jasa Membuat Project Arduino,Robotic,Print 3D ::.

How to use Switch case in Visual Basic

0 komentar


بِسْــــــــــــــــمِ اﷲِالرَّحْمَنِ اارَّحِيم
bismillaahirrahmaanirrahiim

السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ
Assalamu'alaikum warahmatullahi wabarakatuh

How to use Select case in Visual Basic

The switch case doesn�t exist in Visual Basic, is replaced by the select-case statement. The select case is a disguise of the IF statement.  Is not very powerful and I would not recommend it. But using the select-case (switch-case) is good sometimes to use it to make your code easy to read.
Is important to know that the select-case could only evaluate basic and elementary data types (Boolean, double, integer, uinteger, char, �.). reference :

Select...Case Statement (Visual Basic)

Basically, if you want to test an expression in a TextBox1, your select-case should always start like this:


            Select Case TextBox1.Text

               
                Case Else


            End Select



With basic example, I hope you will be able to use the select-case.

using the select-case (switch-case)  equal to something.





            Select Case TextBox1.Text

                Case "1"
                    MsgBox("one")

                Case "2"
                    MsgBox("one")

                Case "3"
                    MsgBox("three")

                Case Else
                    MsgBox("enter a value from 1 to 3 in the textbox")

            End Select



The previous select-case is 100% identical to the if-else statement here:


            If TextBox1.Text = "1" Then
                MsgBox("one")

            ElseIf TextBox1.Text = "2" Then
                MsgBox("one")

            ElseIf TextBox1.Text = "3" Then
                MsgBox("three")

            Else
                MsgBox("enter a value from 1 to 3 in the textbox")

            End If



Please note that the select-case acts like a if-elseif-else statement. The moment the program encounter the first valid expression, it runs whatever inside that valid expression and quits immediately.  In Visual Basic, you don�t need to insert any break expression like in Java or in C#.

use the is in the select case



            Select Case TextBox1.Text

                Case Is < 0
                    MsgBox("is cold")

                Case Is < 18
                    MsgBox("is getting acceptable")

                Case Is < 22
                    MsgBox("is confortable")

                Case Is < 30
                    MsgBox("is getting hot")

                Case Is < 35
                    MsgBox("oh my god")

                Case Is < 40
                    MsgBox("call 911")

                Case Else
                    MsgBox("please enter a valid number in textbox")

            End Select


The previous code is the same if you use the if-elseif-else statement:


    If TextBox1.Text < 0 Then
                MsgBox("is cold")
            ElseIf TextBox1.Text < 18 Then
                MsgBox("is getting acceptable")
            ElseIf TextBox1.Text < 22 Then
                MsgBox("is confortable")
            ElseIf TextBox1.Text < 30 Then
                MsgBox("is getting hot")
            ElseIf TextBox1.Text < 35 Then
                MsgBox("oh my god")
            ElseIf TextBox1.Text < 40 Then
                MsgBox("call 911")
            Else
                MsgBox("please enter a valid number in textbox")
            End If



Use a range using the TO function in the select-case (switch-case)



            Select Case TextBox1.Text

                Case -32000 To 0
                    MsgBox("is cold")

                Case 0 To 21
                    MsgBox("is getting acceptable")

                Case 22 To 30
                    MsgBox("is confortable")

                Case 31 To 34
                    MsgBox("is getting hot")

                Case 35 To 39
                    MsgBox("oh my god")

                Case 40 To 32000
                    MsgBox("call 911")

                Case Else
                    MsgBox("please enter a valid number in textbox")

            End Select



The previous with the use of the TO inside the select-case is equivalent to this code:


            If TextBox1.Text >= -32000 And TextBox1.Text < 0 Then
                MsgBox("is cold")
            ElseIf TextBox1.Text >= 0 And TextBox1.Text < 18 Then
                MsgBox("is getting acceptable")
            ElseIf TextBox1.Text >= 18 And TextBox1.Text < 22 Then
                MsgBox("is confortable")
            ElseIf TextBox1.Text >= 22 And TextBox1.Text < 30 Then
               MsgBox("is getting hot")
            ElseIf TextBox1.Text >= 30 And TextBox1.Text < 35 Then
                MsgBox("oh my god")
            ElseIf TextBox1.Text >= 35 And TextBox1.Text < 40 Then
                MsgBox("call 911")
            Else
                MsgBox("please enter a valid number in textbox")
            End If


Use multiples expressions


Use the comma to separate multiples expressions.


            Select Case TextBox1.Text

                Case -32000 To 0
                    MsgBox("is cold")

                Case 0, 1, 2, 3, 4, 5 To 21
                    MsgBox("is getting acceptable")

                Case 22 To 24, 25 To 30
                    MsgBox("is confortable")

                Case 31, 32, 33, 34
                    MsgBox("is getting hot")

                Case 35 To 39
                    MsgBox("oh my god")

                Case 40, 41, 42, 43, 44, 45, 46, 47, 48 To 32000
                    MsgBox("call 911")

                Case Else
                    MsgBox("please enter a valid number in textbox")

            End Select




Don�t forget to always put the case else statement in case your code goes wrong.

If you like this post, comment it or share it.

Visit my web site at http://checktechno.ca


Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
NB :: Bila Sobat tertarik Ingin membuat software, membeli software, membeli source code, membeli hardware elektronika untuk kepentingan Perusahaan maupun Tugas Akhir (TA/SKRIPSI), Insyaallah Saya siap membantu, untuk Respon Cepat dapat menghubungi kami, melalui :

No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com


atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :

ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِين
Alhamdulilah hirobil alamin

وَ السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ
wassalamualaikum warahmatullahi wabarakatuh


Artikel How to use Switch case in Visual Basic, Diterbitkan oleh scodeaplikasi pada Senin, 26 November 2012. Semoga artikel ini dapat menambah wawasan Anda. Website ini dipost dari beberapa sumber, bisa cek disini sumber, Sobat diperbolehkan mengcopy paste / menyebar luaskan artikel ini, karena segala yang dipost di public adalah milik public. Bila Sobat tertarik Ingin membuat software, membeli software, membeli source code ,Dengan Cara menghubungi saya Ke Email: Fajarudinsidik@gmail.com, atau No Hp/WA : (fajar) : 085369237896, (puat) : 085267792168.

Tawk.to