1

文字列に含まれるものをいくつかチェックし、ケースごとにいくつかの関数を呼び出したい次の選択ケースがあります。ただし、複数の条件が真の場合、最初の条件のみが考慮されるようです。問題は、約 113 の異なるケースがあることです。

ケースごとに if ステートメントを使用する必要がありますか?

   Select Case True
          Case command.ToUpper.Contains(" S:")
'Do something
          Case command.ToUpper.Contains(" C:")
'Do something
          Case command.ToUpper.Contains(" *S")
'Do something
          Case command.ToUpper.Contains(" *C")
'Do something
          Case command.ToUpper.Contains(" CC")
'Do something
          Case command.ToUpper.Contains(" SS")
    End Select
4

2 に答える 2

4

これが select case の定義方法です。一連の If ステートメントを使用するとうまくいきます。

テーブル駆動型のソリューション (疑似コード) を検討してください。

For Each pat In patterns
  If command contains pattern.pat
    perform pattern.action
于 2013-01-06T14:20:51.210 に答える
0

思いつきだけどどうだろう

dim tempCommand as string = command.ToUpper()
dim match as boolean = true

while match andalso tempCommand.length > 0
    select case true
        Case tempCommand.Contains(" S:")
            'Do something
            'then do this    
            tempCommand = tempCommand.replace(" S:","")
        Case tempCommand.Contains(" C:")
            'Do something
            'then do this
            tempCommand = tempCommand.replace(" C:","")
        Case tempCommand.Contains(" *S")
            'Do something
            'then do this
            tempCommand = tempCommand.replace(" *S","")
        Case tempCommand.Contains(" *C")
            'Do something    
            'then do this
            tempCommand = tempCommand.replace(" *C","")
        Case tempCommand.Contains(" CC")
            'Do something
            'then do this
            tempCommand = tempCommand.replace(" CC","")
        Case command.ToUpper.Contains(" SS")
            'then do this
            tempCommand = tempCommand.replace(" SS","")    
        case else match = false
    end select
end while

さまざまな基準に基づいてさまざまなアクションを実行していると想定しているため、「pattern.action の実行」をどのように実行するかはわかりませんが、それが私が知らないコードを動的に実行する方法でない限り、それができれば完全にクールだろう。

私の提案はコマンドを破棄します。そのため、コードでさらに必要な場合に備えて、元のコマンドが失われないように一時保持変数を使用しています。

于 2013-01-28T13:03:35.857 に答える