Endif must be preceded by an if match

I have a piece of vb.net code that I wrote. This is a for loop with two built-in if operations, and the compiler tells me that every elseif and endif should be preceded by an if match.

This is my second day working with vb.net ever, and the only programming experience I have is writing .bat files, so it can be something really stupid. But I can’t understand why I get these errors, and if you all want to help me, I would really 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 

Context for this: I'm trying to write a piece of code that confirms the presence of a bit block. I wrote something in VBScript that would check if the blocker is turned on, and then send an email. This piece of code is part of a program that will extract these emails, compare them with a list of computers and then generate a digest email that indicates which computers are missing, enable or disable the beat blocker, or in an unknown state.

I am sure that there is also a better way to do this, but, as I said, I am pretty new to this, and we need to do this for legal reasons.

Thanks again!

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

+4
source share
4 answers

I would use the built-in syntax in VB.NET with only short and simple conditions. Otherwise, the code becomes less readable and more error prone.

Try the following:

 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 
+4
source

If…Then lines must be broken. 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)) 
Operators

If…Then on one line are autonomous, are not followed by a terminating End If and may not use ElseIf .

+4
source

Your confusion about VB.NET syntax for If statements. VB.NET allows two different formats, each of which has different syntax rules: single-line If statements and multi-line If blocks.

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

 If x Then y = 1 

The multi-line If block is as follows:

 If x Then y = 1 End If 

When you put the code on the same line after Then , it is assumed that you intend to be a single-line If statement. Single-line If statements cannot include ElseIf , not Else ElseIf . They can only be used for simple conditions. Therefore, for your code to work correctly, you need to format it as a multi-line If block, putting the conditional code in the following line, for example:

 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 syntax information, see the MSDN page in the If statement.

+3
source

A single If line should begin 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 
+1
source

Source: https://habr.com/ru/post/1485079/


All Articles