VB.Net中Select Case语句的语法如下:
Select [ Case ] expression
[ Case expressionlist
[ statements ] ]
[ Case Else
[ elsestatements ] ]
End Select
- expression: is an expression that must evaluate to any of the elementary data type in VB.Net, i.e., Boolean, Byte, Char, Date, Double, Decimal, Integer, Long, Object, SByte, Short, Single, String, UInteger, ULong, and UShort.是必须求值到VB.Net中的任何基本数据类型的表达式,即布尔值,字节,字符,日期,双精度,小数,整数,长整型,对象,SByte,短,单,字符串,UInteger, ULong和UShort。
- expressionlist: List of expression clauses representing match values forexpression. Multiple expression clauses are separated by commas.表达式子句表示表达式的匹配值的列表。用逗号分隔多个表达式子句。
- statements: statements following Case that run if the select expression matches any clause in expressionlist.语句如果select表达式匹配expressionlist中的任何子句,则运行的案例。
- elsestatements: statements following Case Else that run if the select expression does not match any clause in the expressionlist of any of the Case statements.如果select表达式与任何Case语句的expression列表中的任何子句不匹配,则运行Case Else后的语句。
示例:
Module decisions
Sub Main()
'定义一个变量
Dim grade As Char
grade = "B"
Select case grade '在.net6下需要加case,不加则自动补全
Case "A"
Console.WriteLine("Excellent!")
Case "B", "C" '多个表达式或者字符串
Console.WriteLine("Well done")
Case "D"
Console.WriteLine("You passed")
Case "F"
Console.WriteLine("Better try again")
Case Else
Console.WriteLine("Invalid grade")
End Select
Console.WriteLine("你的评级为:{0}", grade)
Console.ReadLine()
End Sub
End Module
可以用来做简单的文件类型筛选(不严谨)
© 版权声明
文章版权归作者所有,未经允许请勿转载。