2

为什么下面的代码不起作用?

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")

只是为了测试看看这是否可行......它仍然跳过了分配部分。

我在这里想念什么?

4

5 回答 5

6

这也应该有效:

Select Case fullName
   Case "Example", "Example2", "Example3"
      siteId = 0
End Select
于 2013-01-24T14:43:28.623 回答
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
于 2013-01-24T14:19:46.457 回答
0

试试这个:

Select Case fullName
   Case "Example"
      siteId = 0
   Case "Example2"
      siteId = 0
   Case "Example3"
      siteId = 0
End Select
于 2013-01-24T14:18:44.477 回答
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    
于 2013-01-24T14:41:31.513 回答
0

代替

Select Case fullName

Select Case True
于 2015-08-14T14:38:48.283 回答