Вопрос

I have a piece of vb.net code that I wrote. It's a for loop with two embedded if statements, and the compiler is telling me that each elseif and endif must be preceded by a matching if.

This is my second day of working with vb.net ever, and the only programming experience I have is writing .bat files, so this could be something really stupid. But I cannot figure out why I'm getting these errors, and if you all would be willing to help me out, I would greatly appreciate it!

For Each computer In compArray
        If compArray(I) <> Computers.GetKey(I) Then notpresentList.Add(Computers.GetKey(I)) 
Else 
        If Computers.GetByIndex(I) = 0 Then disabledList.Add(Computers.GetKey(I))
        Elseif Computers.GetByIndex(I)=1 Then enabledList.Add(Computers.GetKey(I))
        Elseif Computers.GetByIndex(I)=2 Then unknownList.Add(Computers.GetKey(I))
    End if
    End if
        I += 1
    Next

The context for this: I am trying to write a piece of code that will confirm the presence of bitlocker. I wrote in VBScript something that will check whether bitlocker is enabled and then send an email. This piece of code is a part of a program which would retrieve those emails, compare them to a list of computers, and then produce a digest email which states which computers are absent, have bitlocker enabled, disabled, or in an unknown state.

I'm sure there's another and better way to do this, but as I said, I'm fairly new at this, and we need to have this done for legal reasons.

Thanks again!

EDIT: If you need more info, please ask me!

Это было полезно?

Решение

I would use the inline syntax in VB.NETonly with short and simple conditions. Otherwise it makes the code less readable and more error-prone.

Try this:

For Each computer In compArray
    If compArray(i) <> Computers.GetKey(i) Then
        notpresentList.Add(Computers.GetKey(i))
    Else
        Dim comp = Computers.GetByIndex(i)
        If comp  = 0 Then
            disabledList.Add(Computers.GetKey(i))
        ElseIf comp = 1 Then
            enabledList.Add(Computers.GetKey(i))
        ElseIf comp = 2 Then
            unknownList.Add(Computers.GetKey(i))
        Else ' added this to show you that this case is not covered yet
            Throw New NotSupportedException
        End If
    End If
    i += 1
Next

Другие советы

Your If…Then lines need to be broken up. Move everything after Then to the next lines and you should be good.

If compArray(I) <> Computers.GetKey(I) Then notpresentList.Add(Computers.GetKey(I)) 

If…Then statements on one line are self-contained, are not followed with a terminating End If, and may not use ElseIf.

Your confusion is in the VB.NET syntax for If statements. VB.NET allows two different formats which each have different syntax rules: Single-line If statements, and Multi-line If blocks.

A single-line If statement looks like this (notice that there is no End If):

If x Then y = 1

A multi-line If block looks like this:

If x Then
    y = 1
End If

When you put code on the same line, after the Then, it assumes that you intend it to be a single-line If statement. Single-line If statements cannot include ElseIf, nor Else conditions. They can only be used for simple conditions. Therefore, to make your code work properly, you need to format it as a multi-line If block by putting the conditional code on the following line, like this:

For Each computer In compArray
    If compArray(I) <> Computers.GetKey(I) Then 
        notpresentList.Add(Computers.GetKey(I)) 
    Else 
        If Computers.GetByIndex(I) = 0 Then 
            disabledList.Add(Computers.GetKey(I))
        Elseif Computers.GetByIndex(I)=1 Then 
            enabledList.Add(Computers.GetKey(I))
        Elseif Computers.GetByIndex(I)=2 Then 
            unknownList.Add(Computers.GetKey(I))
        End if
    End if
    I += 1
Next

For more information on the syntax, take a look at the MSDN page on the If statement.

Single line If's should all start with just If:

i.e.

If Computers.GetByIndex(I) = 0 Then disabledList.Add(Computers.GetKey(I))
If Computers.GetByIndex(I) = 1 Then enabledList.Add(Computers.GetKey(I))
If Computers.GetByIndex(I) = 2 Then unknownList.Add(Computers.GetKey(I))

You can also use Select Case to make it more readable, i.e.

Select Case Computers.GetByIndex(I)

Case 0
  disabledList.Add(Computers.GetKey(I))
Case 1
  enabledList.Add(Computers.GetKey(I))
Case 2
  unknownList.Add(Computers.GetKey(I))
Case Else
 ' Some sort of default action can go here, useful for error catching/prevention
End Select
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top