VB.net中的If,ElseIf,OrElse语法

问题描述 投票:0回答:1

我对某些VB语法有些困惑。我在VB中有这个if / else / if / elseif语句。它可以工作,但环顾四周似乎可以清除。我是来自C#的VB新手,我不太确定如何正确调整它以进行清理。下面是我的代码:

                If taskPath = "\" Then
                    taskPath = Path.GetDirectoryName(TempFilePath)
                Else
                    If InStr(taskPath, "\\") = 1 Then
                    ElseIf InStr(taskPath, "\") = 1 Then
                        taskPath = Path.Combine(Path.GetDirectoryName(TempFilePath), Mid(taskPath, 2, Len(taskPath) - 1))
                    End If
                End If

我想说:

If Foo then
   Bar
ElseIf Foo Then
    OrElse foo then
       bar
End If

除每次尝试这种语法外,我都会收到错误。

我希望这是有道理的,但如果没有,请发表评论,我会尽力澄清

vb.net visual-studio syntax
1个回答
1
投票

语法应为:

If Foo then
   Bar
ElseIf Foo OrElse foo then
   bar
End If

使用您的实际代码,它将变成:

If taskPath = "\" Then
   taskPath = Path.GetDirectoryName(TempFilePath)
ElseIf InStr(taskPath, "\\") = 1 OrElse InStr(taskPath, "\") = 1 Then
   taskPath = Path.Combine(Path.GetDirectoryName(TempFilePath), d(taskPath, 2, Len(taskPath) - 1))
End If
© www.soinside.com 2019 - 2024. All rights reserved.