Android Question IT IS NOT VERY OBVIOUS WHY THIS IS INCORRECT

Intelemarketing

Active Member
Licensed User
Longtime User
I have a Button (Button1) and a Label (Label1) on an Android Screen/Form.

Just to see is B4A works with VERY VERY minor modification, I have added a couple of lines of code


Sub Button1_Click
Label1.text = "Button1 Clicked" <<<<<<<<<<<<<<<<<<<<<<<<<<< THIS IS AN ERROR
FTP.DownloadFile("_ABC/Updates/FTPResult.test", False, File.DirRootExternal, "PRESTATUTI.txt")
Label1.text = "download complete" <<<<<<<<<<<<<<<<<<<<<<<<<<< THIS IS AN ERROR
End Sub

Doesn't like it
Can someone tell me why please ?

 

DonManfred

Expert
Licensed User
Longtime User
abel1.text = "Button1 Clicked" <<<<<<<<<<<<<<<<<<<<<<<<<<< THIS IS AN ERROR
WHICH Error do you get? It does not help us to help you if you hide the error

Upload a small project which shows the problem.
 
Last edited:
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Just guessing...............is Label1 declared in Globals?
B4X:
Private Label1 as Label
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Also, quite aside from this compile error, your code will not do what you probably expect.

FTP.DownloadFile(...) will start the FTP transfer and then immediately return to the next code statement; it will not wait until the download is complete. So the Label1 text will change before the download ends. Look at the example here to see the correct way to use FTP.DownloadFile().
 
Upvote 0

Intelemarketing

Active Member
Licensed User
Longtime User
WHICH Error do you get? It does not help us to help you if you hide the error

Upload a small project which shows the problem.
says "Undeclared Variable 'label1' is used before it was assigned any value
Label1 is not a variable in my understanding. As you can see on the screen it is a CONTROL not a variable.
Unfortunately I am new to B4A and am used to programming in VB6 where you never have to define a label as a variable to use it. (Its a control - not a variable)
Suggestion was to define Label1 in Globals. I cant see the declaration of Button1 in globals. Is a button a different type of control to a label?
Just guessing...............is Label1 declared in Globals?
B4X:
Private Label1 as Label

I have now defined the Label1 in Globals as suggested DIM LABEL1 AS LABEL Red Underline is removed from lines 40 and 42
Where was Button1 defined ?
Is there a list of controls which are controls and those that are not self defining controls ?
This is totally foreign logic to be declaring a label as a variable - some of my forms may have 100 labels - does that mean I need to define each and every label in globals just to use a label ?

Thanks for your patience
 
Upvote 0

udg

Expert
Licensed User
Longtime User
You needed the declaration of the variable Label1 in order to access its property Text.
Button1_click is a method calling so you don't need to declare Button1 unless you have to access its properties too
Read and act on hint from post #6 above.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Where was Button1 defined ?
If you just want to do something with Button1_Click which is an Event Sub, you don't need to declare it in Sub Globals.

some of my forms may have 100 labels - does that mean I need to define each and every label in globals just to use a label ?
You can use a For each loop
B4X:
    Dim i As Int
    For Each v As View In Panel1.GetAllViewsRecursive
        If v Is Label Then
            Dim lbl As Label = v
            i = i + 1
            lbl.Text = "Label " & i           
        End If
    Next
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Label1 is not a variable in my understanding. As you can see on the screen it is a CONTROL not a variable.
It IS a variable that contains a reference (pointer) to the actual view (control). Actually it is the same in VB6. The control or view itself is just an area of memory that contains the values needed to draw and manipulate it. The name of the control is actually a variable pointing to the control that lets you access those values.
 
Upvote 0

Intelemarketing

Active Member
Licensed User
Longtime User
Does that mean that every control that is used in a project needs to be defined as a Global ? I could not see where the Button1 Control was defined as a variable
 
Upvote 0

rraswisak

Active Member
Licensed User
Longtime User
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I have now defined the Label1 in Globals as suggested DIM LABEL1 AS LABEL Red Underline is removed from lines 40 and 42
As already stated in another post, you don't need to write such declaration by hand.
The Designer will do the job for you like this:

- Right Click on the desired View (Label1 in this case)
- Choose Generate
- Choose Dim Label1 as Label (or as B4XView.....but first you need to understand what this is)
Do the same with every View to create even the Click Event, the LongClick Event and every other option available in the various View.
Note that if you have many Views already in the form you can use the Tools -=> Generate Members from the menù in the upper part of the Designer.
From there you will get a complete list of all the views present and you can select everything you need in every View and have the job done in one shot.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Does that mean that every control that is used in a project needs to be defined as a Global ? I could not see where the Button1 Control was defined as a variable
You need to define a View if you need to read or write its Properties at Runtime.
If you need to dinamically change what is written on the Button (or the Label or everything else) then you need to declare it to be able to access the Button.Text Properties (just as an example).
If instead your Button will be static then you can set all its Properties in the Designer (size, color, text......) and you can generate only the Click Event to manage what to do when the user click it.
 
Upvote 0

Intelemarketing

Active Member
Licensed User
Longtime User
Thanks ... Now I get it ... You ARE a COOL CAT !
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…