为什么以下代码不起作用?
Private Function resolveSiteName(ByVal plantName As String, ByVal siteName As String) As Integer
Dim siteId As Integer = Nothing
Dim fullName As String = plantName & siteName
Select Case fullName
Case fullName.Contains("Example" And "Example2" And "Example3")
siteId = 0
End Select
Return siteId
End Function
我猜测我的Select Case fullName
是错误的,因为在调试它时,条件得到满足,它只是跳过分配siteId
。
我也试过这个
Case fullName.Equals("Exactly what full name is")
只是为了测试它是否有效......它仍然跳过了分配部分。
我在这里缺少什么?
答案 0 :(得分:6)
这也应该有效:
Select Case fullName
Case "Example", "Example2", "Example3"
siteId = 0
End Select
答案 1 :(得分:3)
请注意,如果你使用If
- 子句(这里更合适),这甚至都不会编译:
If fullName.Contains("Example" And "Example2" And "Example3") Then
' this doesn't compile since you cannot concat a string with And
' you want to check if fullName equals to one of the strings anyway
siteId = 0
End If
如果要一次检查多个项目,请定义一个集合,将其全部添加到该集合,然后使用Contains
:
Dim siteNames = { "Example", "Example2", "Example3" }
If siteNames.Contains(fullName) Then
siteId = 0
End If
答案 2 :(得分:0)
试试这个:
Select Case fullName
Case "Example"
siteId = 0
Case "Example2"
siteId = 0
Case "Example3"
siteId = 0
End Select
答案 3 :(得分:0)
这解决了您的问题:
Select Case True
Case fullName.Contains("Example")
siteId = 0
End Select
或者第二次尝试:
Select Case fullName
Case "Exactly what full name is"
siteId = 0
End Select
答案 4 :(得分:0)
替换
Select Case fullName
与
Select Case True