If, Elseif, Orelse Syntax in Vb. Net

I am having some confusion with some VB syntax. I have this if/else/if/elseif statement I have in VB. It works, but looking around it seems like this can be cleaned up. I am new to VB coming from a C# background and I am not quite sure how to properly tweak it to clean it up. Below is my code:

                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

I would like to be able to say:

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

Except everytime I try that type of Syntax I get errors.

I hope this makes sense, but if not, leave a comment and I will try to clarify

2

1 Answer

This is how the syntax should be:

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

With your actual code, this becomes:

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
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest