VB.Net/Language Basics/Select

Материал из VB Эксперт
Перейти к: навигация, поиск

Select Case Demo

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
      Dim grade As Integer = 0 " one grade
      Dim aCount As Integer = 0 " number of As
      Dim bCount As Integer = 0 " number of Bs
      Dim cCount As Integer = 0 " number of Cs
      Dim dCount As Integer = 0 " number of Ds
      Dim fCount As Integer = 0 " number of Fs
      Console.Write("Enter a grade, -1 to quit: ")
      grade = Console.ReadLine()
      " input and process grades
      While grade <> -1
         Select Case grade    " determine which grade was input
            Case 100          " student scored 100
               Console.WriteLine("Perfect Score!" & vbCrLf & _
                  "Letter grade: A" & vbCrLf)
               aCount += 1
            Case 90 To 99     " student scored 90-99
               Console.WriteLine("Letter Grade: A" & vbCrLf)
               aCount += 1
            Case 80 To 89     " student scored 80-89
               Console.WriteLine("Letter Grade: B" & vbCrLf)
               bCount += 1
            Case 70 To 79     " student scored 70-79
               Console.WriteLine("Letter Grade: C" & vbCrLf)
               cCount += 1
            Case 60 To 69     " student scored 60-69
               Console.WriteLine("Letter Grade: D" & vbCrLf)
               dCount += 1
               " student scored 0 or 10-59 (10 points for attendance)
            Case 0, 10 To 59
               Console.WriteLine("Letter Grade: F" & vbCrLf)
               fCount += 1
            Case Else
               " alert user that invalid grade was entered
               Console.WriteLine("Invalid Input. " & _
                  "Please enter a valid grade." & vbCrLf)
         End Select
         Console.Write("Enter a grade, -1 to quit: ")
         grade = Console.ReadLine()
      End While
      " display count of each letter grade
      Console.WriteLine(vbCrLf & _
         "Totals for each letter grade are: " & vbCrLf & _
         "A: " & aCount & vbCrLf & "B: " & bCount _
         & vbCrLf & "C: " & cCount & vbCrLf & "D: " & _
         dCount & vbCrLf & "F: " & fCount)
    End Sub
    
End Class


Select Else Demo

Imports System
Public Class MainClass
    Shared Sub Main()
        "Determine which number was entered
        Dim var As Integer
        
        var = 10
        
        Select Case var
            Case 1
                System.Console.WriteLine("The number 1 was entered")
            Case 2
                System.Console.WriteLine("The number 2 was entered")
            Case 3
                System.Console.WriteLine("The number 3 was entered")
            Case 4
                System.Console.WriteLine("The number 4 was entered")
            Case 5
                System.Console.WriteLine("The number 5 was entered")
            Case Else
                System.Console.WriteLine("A number other that 1 - 5 was entered")
        End Select
    End Sub
End Class


Select Integer

Imports System
public class MainClass
    Shared Sub Main()
       Dim targetInteger As Integer = 15
       Select targetInteger
          Case 5
             Console.WriteLine("5")
          Case 10
             Console.WriteLine("10")
          Case 15
             Console.WriteLine("15!")
          Case Else
             Console.WriteLine("Value not found")
       End Select
    End Sub
End Class


Select Integer Value

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
       Dim targetInteger As Integer = 15
       Select Case targetInteger
          Case 5
             Console.WriteLine("5")
          Case 10
             Console.WriteLine("10")
          Case 15
             Console.WriteLine("15!")
          Case Else
             Console.WriteLine("Value not found")
       End Select
    End Sub

End Class


Select range

Imports System
public class MainClass
    Shared Sub Main()
       Dim target As String = "M"
       Select Case target
          Case "A" To "L"
             Console.WriteLine("Alpha To Lambda executed")
          Case "L" To "Z"
             Console.WriteLine("Lambda To Zeta executed")
          Case Else
             Console.WriteLine("Else executed")
       End Select
    End Sub
End Class


Select String Value

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
       Dim target As String = "A"
       Select Case target
          Case "A" To "M"
             Console.WriteLine("A To M executed")
          Case "N" To "Z"
             Console.WriteLine("N To Z executed")
          Case Else
             Console.WriteLine("Else executed")
       End Select
    End Sub

End Class


Select String Value: range

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
       Dim target As String = "A"
       Select Case target
          Case "A" To "M"
             Console.WriteLine("A To M executed")
          Case "N" To "Z"
             Console.WriteLine("N To Z executed")
          Case Else
             Console.WriteLine("Else executed")
       End Select
    End Sub

End Class


Select with Compare

Imports System
Public Class MainClass
    Shared Sub Main(ByVal args As String())
       Dim targetInteger As Integer = 7
       Select Case targetInteger
          Case Is < 10
             Console.WriteLine("Less than 10")
          Case 10 To 14
             Console.WriteLine("10-14")
          Case 15
             Console.WriteLine("15!")
          Case Else
             Console.WriteLine("Value not found")
       End Select
    End Sub

End Class


Select with condition

Imports System
public class MainClass
    Shared Sub Main()
       Dim targetInteger As Integer = 7
       Select Case targetInteger
          Case Is < 10
             Console.WriteLine("Less than 10")
          Case 10 To 14
             Console.WriteLine("10-14")
          Case 15
             Console.WriteLine("15!")
          Case Else
             Console.WriteLine("Value not found")
       End Select
    End Sub
End Class