Android Question If then .. else ... end if

Peekay

Active Member
Licensed User
Longtime User
I am doing this construct a lot of times and use it like:

Code line:
If x=2 then
    lblMessage.text = "It is an even number"
    return
else
    lblMessage.text = "x is not 2"
End if

This construct sometimes gives me an error and sometimes not.

If I have a few code lines after else, it will then always mark the first code line after 'else' saying 'Object reference not set to an instance of an object'.
If I delete that line, it will give the error on the next line after else .. and so on .. always the same error.

I am sure the construct is right, but it picks up an error from something else and always shows it on the same line of the construct.

What could be the cause of it?

PK
 

Keith Flanagan

Member
Licensed User
You probably have warning rather then error's

See the following code:

B4X:
Sub ReturnsValue() As Boolean
    
    Dim x As Int
    Dim lblMessage As String
    
    If x=2 Then
        lblMessage = "It is an even number"
        Return True
    Else
        lblMessage = "x is not 2"
    End If
End Sub

In this case the compiler will warn that Not all code paths return a value since the else should return False.

The code below will remove this warning:

B4X:
Sub ReturnsValue() As Boolean
    
    Dim x As Int
    Dim lblMessage As String
    
    If x=2 Then
        lblMessage = "It is an even number"
        Return True
    Else
        lblMessage = "x is not 2"
        Return False
    End If
End Sub

If you add code after the end If you will also get a warning, Unreachable code detected. That is because the method will return from either the if block or the else block.
B4X:
Sub ReturnsValue() As Boolean
    
    Dim x As Int
    Dim lblMessage As String
    
    If x=2 Then
        lblMessage = "It is an even number"
        Return True
    Else
        lblMessage = "x is not 2"
        Return False
    End If
    
    lblMessage = "Some other value"
End Sub
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
tips: Very Util

IIF( x = 2, "It is an even number", "x is not 2")
or
lblMessage.text = IIF( x = 2, "It is an even number", "x is not 2")

B4X:
Public Sub IIF(InputQuestion As Boolean, OutputTrue As Object, OutputFalse As Object) As Object
    If InputQuestion Then Return OutputTrue Else Return OutputFalse
End Sub

;);););)
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Log(IsBoolean("True")) or Log(IsBoolean(True))

Log(StringToBoolean("True")) or Log(StringToBoolean(True))



B4X:
Public Sub IsBoolean(Value As String) As Boolean
    Return IIF(Value.ToLowerCase = True Or Value.ToLowerCase = False, True, False)
End Sub

Public Sub StringToBoolean(Value As String) As Object
    Return IIF(Value.ToLowerCase = True Or Value.ToLowerCase = False, Value.ToLowerCase, Null)
End Sub
 
Last edited:
Upvote 0

Peekay

Active Member
Licensed User
Longtime User
How do I escape out of the Activity_Create sub from the IF section and proceed after the ELSE section, without using Return?
Here is my code which gives the error in the code line 'MySubFunction=3':

Ifthenelse:
    If CalcRegNo <> StoredRegNo Then
        MySubFunction = 2
        lblSubFunction.text = SubFunctionName(MySubFunction)
        lblMessage.Text = "The entered registration code is incorrect. Enter it again"
        lblMessage.Visible=True
        edtText.visible = True
        btnRegister.visible = True
        Return
    Else
        MySubFunction = 3
        lblSubFunction.text = SubFunctionName(MySubFunction)
        lblMessage.Text = "The App is registered ... please wait for login"
        lblMessage.Visible = True
        edtText.visible = False
        btnRegister.visible = False
        RegistrationOk = True
    End If


PK
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Read:

 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
How do I escape out of the Activity_Create sub from the IF section and proceed after the ELSE section, without using Return?
Here is my code which gives the error in the code line 'MySubFunction=3':
Put the rest of the processing in the Else clause. Are you trying to do this?
B4X:
If CalcRegNo <> StoredRegNo Then
   'Set up failure conditions
   Return
Else
   ' We're good to go
End If
'Now let's do some more processing here if we're successful

If so, then just do
B4X:
If CalcRegNo <> StoredRegNo Then
   'Set up failure conditions
   'NOTE: No return required here
Else
   ' We're good to go
   'NOTE: Do the rest of the processing here
   'Now let's do some more processing here if we're successful
End If
'NOTE: If necessary, do any processing here that would apply to both success and failure cases
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Do you have any module sharing the same name with a variable in your "else" segment?
 
Upvote 0

Peekay

Active Member
Licensed User
Longtime User
Oliver,
'NOTE: If necessary, do any processing here that would apply to both success and failure cases
I specifically want to escape from the whole sub routine if it does not meet the first condition, which is the object of these forks.
I think it has to do with using a Return in the first part, but not in the second part.

mc&3,
No I do not have any.

I am just verifying a very simple condition, contained in the first line of the code I have given.
I have solved it by doing two if statements, one of the condition being true and the other of the condition being false, but that should not be necessary.
This is not the first time I have found this condition of an error whilst the error was somewhere else.
I realise that this is not easy to solve from inspecting the the routine in isolation.
However, thank you all for your appreciated advice.

PK
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Oliver,
I specifically want to escape from the whole sub routine if it does not meet the first condition, which is the object of these forks.
I think it has to do with using a Return in the first part, but not in the second part.
Then don't do anything after the End If and it will do as you intend. You just do all the processing in the Else clause (in your example). In the setup I I have shown you, you would not need a Return statement at all (for your specific case).

have solved it by doing two if statements, one of the condition being true and the other of the condition being false,
Why? That is the whole reason for the Else clause of an If statement! Just saying...
 
Upvote 0
Top