VB.Net Tutorial/Statements/Select — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:53, 26 мая 2010
Содержание
- 1 Select Case statement with Integer and exact value
- 2 Select Case statement with range Integer value
- 3 Select Case statement with String range value
- 4 Select Now.DayOfWeek
- 5 Specify several values in one Case statement
- 6 Use both range and fix value in Select statement
- 7 Using Select Case structure
Select Case statement with Integer and exact value
Option Strict On
Imports System
Module Module1
Sub Main( )
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 "Main
End Module
15!
Select Case statement with range Integer value
Option Strict On
Imports System
Module Module1
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 "Main
End Module
Less than 10
Select Case statement with String range value
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim target As String = "M"
Select Case target
Case "A" To "L"
Console.WriteLine("A To L executed")
Case "L" To "Z"
Console.WriteLine("L To Z executed")
Case Else
Console.WriteLine("Else executed")
End Select
End Sub "Main
End Module
L To Z executed
Select Now.DayOfWeek
public class Test
public Shared Sub Main
Dim strMessage As String
Select Case Now.DayOfWeek
Case DayOfWeek.Monday
strMessage = "Have a nice week."
Case DayOfWeek.Friday
strMessage = "Have a nice weekend."
Case Else
strMessage = "Welcome back!"
End Select
Console.WriteLine(strMessage)
End Sub
End class
Have a nice weekend.
Specify several values in one Case statement
public class Test
public Shared Sub Main
Select Case "Bird"
Case "Bird"
Console.WriteLine("This animal has 2 legs.")
Case "Horse", "Dog", "Cat"
Console.WriteLine("This animal has 4 legs.")
Case "Snake"
Console.WriteLine("This animal has no legs.")
Case "Centipede"
Console.WriteLine("This animal has 100 legs.")
Case Else
Console.WriteLine("You did not select from the list!")
End Select
End Sub
End Class
This animal has 2 legs.
Use both range and fix value in Select statement
Module Module1
Sub Main()
Dim intInput As Integer = 12
Select Case intInput
Case 1
Console.WriteLine("Thank you for the 1.")
Case 2 To 5
Console.WriteLine("Your value was 2, 3, 4, or 5")
Case Is > 5
Console.WriteLine("That was greater than 5.")
Case Else
Console.WriteLine("Sorry, I can"t deal with that.")
End Select
End Sub
End Module
That was greater than 5.
Using Select Case structure
Module Tester
Sub Main()
Dim grade As Integer = 89
Dim aCount As Integer = 0
Dim bCount As Integer = 0
Dim cCount As Integer = 0
Dim dCount As Integer = 0
Dim fCount As Integer = 0
Select Case grade
Case 100
Console.WriteLine("Perfect Score!" & vbCrLf & "Letter grade: A" & vbCrLf)
aCount += 1
Case 90 To 99
Console.WriteLine("Letter Grade: A" & vbCrLf)
aCount += 1
Case 80 To 89
Console.WriteLine("Letter Grade: B" & vbCrLf)
bCount += 1
Case 70 To 79
Console.WriteLine("Letter Grade: C" & vbCrLf)
cCount += 1
Case 60 To 69
Console.WriteLine("Letter Grade: D" & vbCrLf)
dCount += 1
Case 0, 10 To 59
Console.WriteLine("Letter Grade: F" & vbCrLf)
fCount += 1
Case Else
Console.WriteLine("Invalid Input. " & _
"Please enter a valid grade." & vbCrLf)
End Select
End Sub
End Module
Letter Grade: B