Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 有条件地设置字符串_Vb.net_Conditional Statements - Fatal编程技术网

Vb.net 有条件地设置字符串

Vb.net 有条件地设置字符串,vb.net,conditional-statements,Vb.net,Conditional Statements,我不知道如何确切地解释我的问题,但我会尽我所能 我有两个字符串:string1,string2 现在我想更新它们中的任何一个,条件是整数condit必须为-1 我有这个代码,我想减少它 If condInt <> -1 Then If (optimizationSelector = Integer.MaxValue) Then string1 = MainString.Substring(whereSelector + 6)

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

我有两个字符串:
string1
string2
现在我想更新它们中的任何一个,条件是整数
condit
必须为-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
如果条件为-1,则
如果(optimizationSelector=Integer.MaxValue),则
string1=MainString.Substring(其中选择器+6)
其他的
string1=MainString.Substring(whereSelector+6,optimizationSelector-(whereSelector+6))
如果结束
其他的
如果optimizationSelector=Integer.MaxValue,则
string2=MainString.Substring(来自选择器+5)
其他的
string2=MainString.Substring(fromSelector+5,optimizationSelector-(fromSelector+5))
如果结束
如果结束
现在我使用以下代码替换一些代码:
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
Dim STARTVALUETUSE=IIf(其中选择器-1,其中选择器+6,来自选择器+5)
如果condit-1那么
如果(optimizationSelector=Integer.MaxValue),则
string1=MainString.Substring(startValueToUse)
其他的
string1=MainString.Substring(startValueToUse,optimizationSelector-(startValueToUse))
如果结束
其他的
如果optimizationSelector=Integer.MaxValue,则
string2=MainString.Substring(startValueToUse)
其他的
string2=MainString.Substring(startValueToUse,optimizationSelector-(startValueToUse))
如果结束
如果结束
我想也许我可以通过使用
Iif(condit-1,string1,string2)
替换string1和string2来删除if。因此,这将导致:

    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
Dim STARTVALUETUSE=IIf(其中选择器-1,其中选择器+6,来自选择器+5)
如果(optimizationSelector=Integer.MaxValue),则
Iif(第1条、第1条、第2条)=
主字符串。子字符串(起始值使用)
其他的
Iif(第1条、第1条、第2条)=
主字符串.子字符串(startValueToUse,optimizationSelector-(startValueToUse))
如果结束

但结果是一个错误,这是不可能的。。我该怎么做才能使这样的事情成为可能?

不,不能使用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
如果条件为-1,则
string1=If(optimizationSelector=Integer.MaxValue_
主字符串。子字符串(其中选择器+6)_
MainString.Substring(whereSelector+6,optimizationSelector-(whereSelector+6)))
其他的
string2=If(optimizationSelector=Integer.MaxValue_
主字符串。子字符串(从选择器+5)_
MainString.Substring(fromSelector+5,optimizationSelector-(fromSelector+5)))
如果结束
您的问题在于,您需要根据变量的条件初始化两个不同的字符串变量
condit
,因此您需要两个不同的情况

您可以尝试以下方法:

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

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

你不能那样使用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
如果条件为-1,则
索引=1
其他的
指数=2
如果结束
如果(optimizationSelector=Integer.MaxValue),则
stringValue(索引)=主字符串。子字符串(起始值)
其他的
stringValue(索引)=主字符串.子字符串(startValueToUse,optimizationSelector-(startValueToUse))
如果结束
如果没有,可以将该值放置在临时位置

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
If(optimizationSelector=Integer.MaxValue)则
tempString=MainString.Substring(startValueToUse)
其他的
tempString=MainString.Substring(startValueToUse,optimizationSelector-(startValueToUse))
如果结束
如果condit-1那么
string1=TempString
其他的
string2=tempString
如果结束

我需要两个字符串分开,因为这两个字符串最终会产生两个不同的对象。因为您没有同时使用string1和string2(Selector不能是=-1和-1),所以我只选择一个变量。根据变量result的值,您仍然可以实例化2个不同的对象。我以为Iif是类似Vb的三元运算符。从现在起,我将使用If,谢谢。