有条件地设置字符串

时间:2014-04-04 12:45:53

标签: vb.net conditional-statements

我不知道如何解释我的问题,但我会尽我所能。

我有两个字符串:string1string2 现在我想更新其中任何一个,给出条件整数condInt需要为-1。

我有这个代码,我想减少它..

If condInt <> -1 Then
            If (optimizationSelector = Integer.MaxValue) Then
                string1 = MainString.Substring(whereSelector + 6)
            Else
                string1 = MainString.Substring(whereSelector + 6, optimizationSelector - (whereSelector + 6))
            End If
        Else
            If optimizationSelector = Integer.MaxValue Then
                string2 = MainString.Substring(fromSelector + 5)
            Else
                string2 = MainString.Substring(fromSelector + 5, optimizationSelector - (fromSelector + 5))
            End If
        End If

现在我使用:Dim startValueToUse = IIf(whereSelector <> -1, whereSelector + 6, fromSelector + 5)

替换一些代码

结果是:

Dim startValueToUse = IIf(whereSelector <> -1, whereSelector + 6, fromSelector + 5)
If condInt <> -1 Then
            If (optimizationSelector = Integer.MaxValue) Then
                string1 = MainString.Substring(startValueToUse )
            Else
                string1 = MainString.Substring(startValueToUse , optimizationSelector - (startValueToUse ))
            End If
        Else
            If optimizationSelector = Integer.MaxValue Then
                string2 = MainString.Substring(startValueToUse )
            Else
                string2 = MainString.Substring(startValueToUse , optimizationSelector - (startValueToUse))
            End If
        End If

我想也许我可以通过替换string1&amp;删除if string2使用Iif(condInt <> -1, string1, string2)。所以它会导致:

    Dim startValueToUse = IIf(whereSelector <> -1, whereSelector + 6, fromSelector + 5)
                If (optimizationSelector = Integer.MaxValue) Then
                    Iif(condInt <> -1, string1, string2) =
MainString.Substring(startValueToUse )
                Else
                    Iif(condInt <> -1, string1, string2) =
MainString.Substring(startValueToUse , optimizationSelector - (startValueToUse ))
                End If

但结果是错误,这是不可能的......我能做些什么才能做出这样的事情呢?

3 个答案:

答案 0 :(得分:0)

不,你不能在equals运算符的左侧使用IIF函数 另外,我建议删除IIF函数并使用IF三元运算符语法来短路代码评估

    If condInt <> -1 Then
        string1 = If(optimizationSelector = Integer.MaxValue, _
                     MainString.SubString(whereSelector + 6), _
                     MainString.Substring(whereSelector + 6, optimizationSelector - (whereSelector + 6)))
    Else
        string2 = If(optimizationSelector = Integer.MaxValue, _
                  MainString.Substring(fromSelector + 5), _
                  MainString.Substring(fromSelector + 5, optimizationSelector - (fromSelector + 5)))
    End If

您的问题是您希望根据变量condInt的条件初始化两个不同的字符串变量,因此您需要两种不同的情况

答案 1 :(得分:0)

你可以试试这个:

Dim startValueToUse = If(whereSelector <> -1, whereSelector + 6, fromSelector + 5)
Dim result = If(optimizationSelector = Integer.MaxValue, MainString.Substring(startValueToUse), MainString.Substring(startValueToUse, optimizationSelector - startValueToUse))

它计算变量result中的结果,删除string1和string2。你真的需要两者兼而有之吗?

答案 2 :(得分:0)

你不能像那样使用iif。如果您的字符串实际上是名称string1和string2,那么您可以使用数组。

If condInt <> -1 Then
    index = 1
Else
    index = 2
End If


If (optimizationSelector = Integer.MaxValue) Then
    stringValue(index) = MainString.Substring(startValueToUse )
Else
    stringValue(index) = MainString.Substring(startValueToUse , optimizationSelector - (startValueToUse ))
End If

如果没有,您可以将值放在临时位置

If (optimizationSelector = Integer.MaxValue) Then
    tempString = MainString.Substring(startValueToUse )
Else
    tempString = MainString.Substring(startValueToUse , optimizationSelector - (startValueToUse ))
End If

If condInt <> -1 Then
    string1 = TempString
Else
    string2 = tempString
End If