Rifqi Rizqullah Rifqi Rizqullah - 4 months ago 13
Vb.net Question

elseif must be precedeed by a matching if or elseif?

I'm trying to find one category that is based on two different classification with this code but i got notifications that "elseif must be precedeed by a matching if or elseif" on the first two classification i made.

if pof < 0.1 then CCat = "A"
elseif 0.1 < pof < 0.2 then CCat = "B"
elseif 0.2 < pof < 0.3 then CCat = "C"
elseif 0.3 < pof < 0.5 then CCat = "D"
else CCat = "E"
end if

if cof < 10000 then CCat = "A"
elseif 10000 < cof < 50000 then CCat = "B"
elseif 50000 < cof < 150000 then CCat = "C"
elseif 150000 < cof < 1000000 then CCat = "D"
else CCat = "E"
end if


is there a problem with my if conditions? pof and cof are both a double resulting from a calculation done before.

Answer

It's just a little syntactic trip-up you're hitting. If you drop the code after the "Then" operator down to the next line, VB considers it a multi-line statement, rather than a single-line statement. That's when you get ElseIf, Else, and End If

I am betting you're used to C#, where you can map it all out with brackets. {}

It took me a few moments to remember VB enough, but here's what I think you want:

    If pof < 0.1 Then
        CCat = "A"
    ElseIf 0.1 < pof < 0.2 Then
        CCat = "B"
    ElseIf 0.2 < pof < 0.3 Then
        CCat = "C"
    ElseIf 0.3 < pof < 0.5 Then
        CCat = "D"
    Else
        CCat = "E"
    End If


    If cof < 10000 Then
        CCat = "A"
    ElseIf 10000 < cof < 50000 Then
        CCat = "B"
    ElseIf 50000 < cof < 150000 Then
        CCat = "C"
    ElseIf 150000 < cof < 1000000 Then
        CCat = "D"
    Else
        CCat = "E"
    End If
Comments