Android Question A NewBie at B4A

Discussion in 'Android Questions' started by pokhraj_d, Feb 14, 2019.

  1. pokhraj_d

    pokhraj_d Member

    Hello All,
    I am newbie at B4A and Not able to understand the below error.
    When first time I click on Check button after compile it is showing the correct result, but after that every time the app showing the result is wrong.
    Am I missing something. Please suggest.
    Below is my code
    Code:
    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.

    End Sub

    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.

        
    Private Button1 As Button
        
    Private Label3 As Label
        
    Private lblnumber1 As Label
        
    Private lblnumber2 As Label
        
    Private lblResult As Label
        
    Private txt1 As EditText
        
    Public num1,num2 As Int
        
    Private btnNumcreate As Button
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    'Do not forget to load the layout file created with the visual designer. For example:
        'Activity.LoadLayout("Layout1")
        Activity.LoadLayout("calculator")
        New
    End Sub

    Sub New
        num1=
    Rnd(1,10)
        num2=
    Rnd(1,10)
        lblnumber1.Text=num1
        lblnumber2.Text=num2
        lblResult.Text=
    ""
    End Sub
    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    Sub Button1_Click()
        
    If txt1.Text="" Then
            lblResult.Text=
    ""
            
    Msgbox("No Result Entered!","ERROR")
        
    Else
            CheckResult
        
    End If
    End Sub

    Sub CheckResult
        
    If txt1.Text=(num1+num2) Then
            lblResult.Text=
    "Correct!"
        
    Else
            lblResult.Text=
    "Wrong!"
        
    End If
    End Sub

    Sub btnNumcreate_Click()
        lblnumber1.Text=
    Rnd(1,10)
        lblnumber2.Text=
    Rnd(1,10)
        lblResult.Text=
    ""
        txt1.Text=
    ""
    End Sub
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Use Log instead of Msgbox.

    What is the expected output and what is the actual output?
     
  3. klaus

    klaus Expert Licensed User

    I think that the problem is here:
    Code:
    Sub btnNumcreate_Click()
        lblnumber1.Text=
    Rnd(1,10)
        lblnumber2.Text=
    Rnd(1,10)
        lblResult.Text=
    ""
        txt1.Text=
    ""
    End Sub
    It should be:
    Code:
    Sub btnNumcreate_Click()
        num1=
    Rnd(1,10)
        num2=
    Rnd(1,10)
        lblnumber1.Text=num1
        lblnumber2.Text=num2
        lblResult.Text=
    ""
        txt1.Text=
    ""
    End Sub
    Or simply:
    Code:
    Sub btnNumcreate_Click()
        New
    End Sub
     
    Last edited: Feb 14, 2019
    Erel, Jeffrey Cameron and OliverA like this.
  4. pokhraj_d

    pokhraj_d Member

    Erel,
    Thank you. It is working as expected

    Thanks-
    Pokhraj
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice