Sub Referencing Itself

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Other languages might call it something like Self or This...although rare in a BASIC language in the area I'm wondering since it is the Return value, but is something like this possible (Lame example, but gets the point across):

B4X:
Sub CustomButton(btnText As String) As Button
   CustomButton.Initialize("btnClick")
   CustomButton.Text = btnText
   CustomButton.Typeface = Typeface.DEFAULT_BOLD
   CustomButton.Gravity = Gravity.CENTER
End Sub

The sub returns a button, so I'm just curious if it is an object yet or is it always required to dim a button in the function and then return it. Typing the code above makes it look like it is possible since the list comes up when I type "CustomButton.", but on compile it reports missing () like it is only a sub.
 

wl

Well-Known Member
Licensed User
Longtime User
Guess this is impossible because you are using a sub as an object ... I think you need to dim a new button, sets its properties etc and return it.
 
Upvote 0

Jost aus Soest

Active Member
Licensed User
Longtime User
You must write it in this way:
B4X:
Sub CustomButton(btnText As String, EventName As String) As Button
  Dim b As Button
  b.Initialize(EventName)
  b.Text = btnText
  b.CustomButton.Typeface = Typeface.DEFAULT_BOLD
  b.CustomButton.Gravity = Gravity.CENTER
  Return b
End Sub
 
Upvote 0
Top