What does .initialize do?

barx

Well-Known Member
Licensed User
Longtime User
As per title, what does xxyy.initialize actually do?

The reason I ask is that I'm getting a little confused (Maybe a note for the n00b questions post) when and when not to use .initialize. I get it in the end as the IDE reports errors if I forget them.

Now, the syntax is xxyy.initialize("event name") when referencing the control in code do you use the event name or the initial variable. Up to now I've kept the variable and event name the same so no issue, it's more of a curiousity than an problem. i.e with the following code:

Dim txtData as EditText

...

txtData.initialize("TextData")
...


Would you later reference it as txtData or TextData?
I would probably never use above method but knowing this would help me understand more.

One last thing, couldn't the .initialize code be automated. After a 'Dim xxxx as Button' auto add 'xxxx.initialize("xxxx")' at the top of Activity_Create.

Enough babble from me for now ;)

keep up the good work,

cheers.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Initialize prepares the object. The actual action differs between different objects. Some objects do not need any initialization at all.

Views added with the designer should not be initialized. Activity.LoadLayout initialized the views for you (this is a common mistake).

The EventName parameter wires the event Subs to the object events.
For example:
B4X:
Dim Button1 As Button
Button1.Initialize("abcd")
Activity.AddView(Button1, 10dip, 10dip, 200dip, 200dip)
Button1.Text = "Go!"
...
Sub abcd_Click
 Msgbox("hello world")
End Sub
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Initialize prepares the object. The actual action differs between different objects. Some objects do not need any initialization at all.

Views added with the designer should not be initialized. Activity.LoadLayout initialized the views for you (this is a common mistake).

The EventName parameter wires the event Subs to the object events.
For example:
B4X:
Dim Button1 As Button
Button1.Initialize("abcd")
Activity.AddView(Button1, 10dip, 10dip, 200dip, 200dip)
Button1.Text = "Go!"
...
Sub abcd_Click
 Msgbox("hello world")
End Sub

Wow, that could get confusing then to set them different. So, you would set the proterties by refering to it as Button1 but catch events using it's 'alias' abcd. Think I'll stick to keeping them the same. Is there any adventage of choosing differeing names?

Thanks for the info though. Helped clear it up in my head.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
The advantage comes when you want to set a group of buttons to call the same click event, as they are all doing similar things (think number buttons on a calculator).
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Ah, I get you. So you have Button1, Button2, etc. Then initialize them oil same event name. All sinking in now. So what method would you use in the event to distinguish between them? Read their .text property? OR is this were the .tag is used. Also wondered what that does lol

Sent from my HTC Vision using Tapatalk
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Thanks Erel. Will do some reading.

Sent from my HTC Vision using Tapatalk
 
Last edited:
Upvote 0

aklisiewicz

Active Member
Licensed User
Longtime User
This is the most confusing concept for me, as I was always reffering to something on the screen by its name. So how do I reffer to screen views ?
If I have TestBox1 and TexBox2 and I want to compare them, or I want assign TextBox2 to a column in database ?
If there is some more documentation on those concepts it would save us lots of sweeting and you will not have to answer those questions again and again...

Art
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Ah, I get you. So you have Button1, Button2, etc. Then initialize them oil same event name. All sinking in now. So what method would you use in the event to distinguish between them? Read their .text property? OR is this were the .tag is used. Also wondered what that does lol
Have a look at the Beginners Guide, there is an example with buttons handled in a same Click event routine, there you will see the advantage.

Best regards.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Ah, I get you. So you have Button1, Button2, etc. Then initialize them oil same event name. All sinking in now. So what method would you use in the event to distinguish between them? Read their .text property? OR is this were the .tag is used. Also wondered what that does lol

Sent from my HTC Vision using Tapatalk

I find the .tag to be useful. This code snippet is called by ten buttons (0 to 9) with their tag properties set to the number I want the button to use:
B4X:
Sub SetArrow_Click
   Dim l As Button
   Dim value As Int 
   l = Sender
   value=l.Tag 
   arrows(pointer)=value
End Sub
So I have "l" as a temp button and assign "Sender" to it, this is the view that called the event.
I can then get the tag value and use it however I want.

It is worth noting that the tag can store (almost) anything, so it could be a panel, an array, custom type etc. It is a very flexible system once mastered.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Have a look at the Beginners Guide, there is an example with buttons handled in a same Click event routine, there you will see the advantage.

Best regards.

Yeah, I'm reading through the Beginners Guide, I guess I'm a little too eager and jumping the gun a little. enjoying it so far though. Like to give myself a decent learning curve to follow ;)
 
Upvote 0
Top