0

以下のコードは機能しますが、これを行うためのより良い方法があると思わずにはいられません。関数を Select ステートメントと組み合わせて使用​​した経験がある人。
私が期待するコードは、線に沿ったものになるでしょう...

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

このコードは機能します:

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
4

2 に答える 2

0

.EndsWith()true または false を返します。それだけです。

それを使用したい場合Select、慣用的な方法は

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

同じ行に複数の選択肢があっても、大きな違いはありません。

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

配列/リスト/コレクションに既に選択肢のリストがある場合は、次を使用することもできます

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

または、必要に応じて同じインラインを実行します。

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
于 2012-10-19T19:48:58.137 に答える
0

2 つのケースの唯一の違いは、sbSubreportアイテムの追加です。これは特別なチェックが必要な唯一の項目であり、次のように行うことができます

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
于 2012-10-19T19:49:16.353 に答える