Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Endif must be preceded by a matching if

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!

like image 281
Wolves Avatar asked Jun 07 '13 15:06

Wolves


1 Answers

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
like image 55
Tim Schmelter Avatar answered Nov 07 '22 16:11

Tim Schmelter