2

Why doesn't the following code work?

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

I'm guessing my Select Case fullName is wrong, because when debugging it, the conditions are met and it just skips over assigning the siteId.

I also tried just this

Case fullName.Equals("Exactly what full name is")

just to test to see if that would work...and it still skipped over the assigning part.

What am I missing here?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Brandon
  • 895
  • 4
  • 21
  • 43

5 Answers5

6

This should work too:

Select Case fullName
   Case "Example", "Example2", "Example3"
      siteId = 0
End Select
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Ric
  • 11,643
  • 3
  • 24
  • 35
3

Note that this would not even compile if you'd use an If-clause(which is more appropriate here):

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

If you want to check multiple items at once, define a collection, add all to it and then use Contains:

Dim siteNames = { "Example", "Example2", "Example3" }
If siteNames.Contains(fullName) Then
    siteId = 0
End If
Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
0

Try this:

Select Case fullName
   Case "Example"
      siteId = 0
   Case "Example2"
      siteId = 0
   Case "Example3"
      siteId = 0
End Select
Steven Ackley
  • 593
  • 6
  • 29
0

This solves your problem:

Select Case True
    Case fullName.Contains("Example")
        siteId = 0
End Select

Or for your second try:

Select Case fullName
    Case "Exactly what full name is"
        siteId = 0
End Select    
SysDragon
  • 9,193
  • 15
  • 53
  • 86
0

Replace

Select Case fullName

with

Select Case True
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123