ruedi
ruedi

Reputation: 5555

do not understand if else block behaviour

I have the following code:

Sub Main()
    Dim a As Integer = 8 * 60
    Dim b As Integer
    Dim c As Integer
    If a < (6 * 60) Then
        b = 0 And c = 0
    ElseIf a >= 6 * 60 And a < 9 * 60 Then
        b = 30 And c = 1
    Else
        b = 45 And
       c = 1
    End If
    MsgBox(b)
End Sub

Thinks i dont understand and where i need someones help:

  1. "c=0" and "c=1" are underlined with the error: Strict on doesnt allow implicit convertation from boolean to integer. WHY? I declared c as integer!
  2. Variable "b" and "c" are always "0" even though in the case above they should be b=30 and c = 1.

can anyone please explain me this behaviour.

Upvotes: 0

Views: 53

Answers (1)

Dennis Traub
Dennis Traub

Reputation: 51654

You are using the And keyword where it is not allowed. And is a logical operator (along with Or, AndAlso, OrElse.)

The following should work.

Sub Main()
    Dim a As Integer = 8 * 60
    Dim b As Integer
    Dim c As Integer
    If a < (6 * 60) Then
        b = 0
        c = 0
    ElseIf a >= 6 * 60 And a < 9 * 60 Then
        b = 30
        c = 1
    Else
        b = 45
        c = 1
    End If
    MsgBox(b)
End Sub

Upvotes: 2

Related Questions