在VB.NET中选择Case True

在VB.NET中选择Case True,.net,vb.net,select-case,.net,Vb.net,Select Case,我有下面的select案例,我想对字符串包含的内容进行一些检查,并为每个案例调用一些函数。但是,如果多个条件为真,它似乎只考虑第一个条件。问题是我有大约113个不同的案例 我是否必须为每种情况使用if语句 Select Case True Case command.ToUpper.Contains(" S:") 'Do something Case command.ToUpper.Contains(" C:") 'Do something

我有下面的select案例,我想对字符串包含的内容进行一些检查,并为每个案例调用一些函数。但是,如果多个条件为真,它似乎只考虑第一个条件。问题是我有大约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

这就是select case的定义方式。使用一系列If语句将有效

考虑一个表驱动的解决方案伪代码:

For Each pat In patterns
  If command contains pattern.pat
    perform pattern.action

这就是select case的定义方式。使用一系列If语句将有效

考虑一个表驱动的解决方案伪代码:

For Each pat In patterns
  If command contains pattern.pat
    perform pattern.action

只是一个想法,但是你呢

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
我假设根据不同的标准,您正在执行不同的操作,因此我不确定您将如何执行perform pattern.action,除非这是一种动态执行代码的方法,我不知道,如果可以的话,这将是非常酷的


我的建议确实破坏了该命令,这就是为什么我使用了一个临时的holding变量,以便原始命令不会丢失,以防您在代码中进一步需要它。

只是一个想法,但是呢

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
我假设根据不同的标准,您正在执行不同的操作,因此我不确定您将如何执行perform pattern.action,除非这是一种动态执行代码的方法,我不知道,如果可以的话,这将是非常酷的


我的建议确实破坏了该命令,这就是为什么我使用了一个临时的holding变量,以便原始命令不会丢失,以防您在代码中需要它。

@Oded完全不同的东西,最后,它们将用于生成一个dynamc sql命令,该命令返回一个用于绘制数据的表@Oded完全不同的数据可能重复,最后它们将用于生成一个dynamc sql命令,该命令返回一个用于绘制数据的表可能重复的数据