0

The code below works, I can't help thinking there is a better way to do this though. Is anyone experienced with using functions in conjunction with Select Statements.
The code I would expect to work would be something along the lines...

Select Case File.EndsWith()
Case "example 1", Case "example2"

This code works:

Select Case File.EndsWith(File)
    Case tFile.EndsWith("FileA.doc")
        sbExisting.AppendLine(Report.sbStart.ToString)
        sbExisting.AppendLine(Report.sbHeaders.ToString)
        sbExisting.AppendLine(Report.sbItems.ToString)
        sbExisting.AppendLine(Report.sbSubreport.ToString)
        sbExisting.AppendLine(Report.sbEnd.ToString)
        sbExisting.AppendLine(Report.sbCol.ToString)
    Case tFile.EndsWith("FileB.doc")
        'Slave
        sbExisting.AppendLine(Report.sbStart.ToString)
        sbExisting.AppendLine(Report.sbItems.ToString)
        sbExisting.AppendLine(Report.sbHeaders.ToString)
        sbExisting.AppendLine(Report.sbCol.ToString)
        sbExisting.AppendLine(Report.sbEnd.ToString)
End Select
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
Ccorock
  • 892
  • 12
  • 37
  • I think just `Select Case True` would do it, instead of `Select Case tFile.EndsWith(tFile)` (which will always be true anyway) – Wug Oct 19 '12 at 19:49
  • It actually works as expected with the code above; Properly distinguishing against either case. – Ccorock Oct 19 '12 at 19:54
  • 1
    Just because something works as expected does not mean it's efficient or simple. – Wug Oct 19 '12 at 19:57
  • 1
    I couldn't agree more. I was simply clarifying that the code itself works correctly. The intent of my post is to find an eloquent solution. – Ccorock Oct 19 '12 at 20:00

2 Answers2

0

.EndsWith() returns true or false. That's all you have.

If you do want to use Select with that, then the idiomatic way is

Select Case True
    Case tFile.EndsWith("MSMaster.tmp")
        ...
    Case tFile.EndsWith("MSSlave.tmp")
        ...
End Select

Having multiple choices on the same line would be of not much difference:

Select Case True
    Case tFile.EndsWith("example 1"), tFile.EndsWith("example 2")
        ...
    Case tFile.EndsWith("example 3"), tFile.EndsWith("example 4")
        ...
End Select

If you already have a list of choices in an array/list/collection, you can also use

Dim choices1 = New String() {"example 1", "example 2"}
Dim choices2 = New String() {"example 3", "example 4"}

Select Case True
    Case choices1.Any(Function(s) tFile.EndsWith(s))
        ...
    Case choices2.Any(Function(s) tFile.EndsWith(s))
        ...
End Select

Or do the same inline if you prefer:

Select Case True
    Case (New String() {"example 1", "example 2"}).Any(Function(s) tFile.EndsWith(s))
        ...
    Case (New String() {"example 3", "example 4"}).Any(Function(s) tFile.EndsWith(s))
        ...
End Select
GSerg
  • 76,472
  • 17
  • 159
  • 346
0

The only difference between the 2 cases is the appending of the sbSubreport item. That is the only item which needs a special check and can be done like the following

Dim master = tFile.EndsWith("MSMaster.tmp")
Dim slave = tFile.EndsWith("MSSlave.tmp")
If master OrElse slave Then
    sbExisting.AppendLine(Report.sbStart.ToString)
    sbExisting.AppendLine(Report.sbHeaders.ToString)
    sbExisting.AppendLine(Report.sbItems.ToString)
    If master Then
        sbExisting.AppendLine(Report.sbSubreport.ToString)
    End If
    sbExisting.AppendLine(Report.sbEnd.ToString)
    sbExisting.AppendLine(Report.sbCol.ToString)
End If
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    The code is only a prototype to the actual project. This solution would only work for the specific circumstance. Interesting all the same. – Ccorock Oct 19 '12 at 19:51