Classes are soon coming...

Erel

B4X founder
Staff member
Licensed User
Longtime User
Basic4android v2.00 will include support for classes.

Classes are like templates which you can use to instantiate many objects.
Users who are used to object oriented programming will sure find it easier to develop and maintain large projects using classes.

Classes are also useful for creating reusable components.

You can think of classes as structures defined with Type keyword which also include subs. However classes are more sophisticated as they also store a reference to the activity context or the process context. This allows you to consume events inside classes. It also allows you to use CallSub with classes objects.

OOP characteristics:
Encapsulation - new Private and Public scope keywords.
Polymorphism - Duck Typing with SubExist (new keyword) and CallSub keywords.
Inheritance is not implemented for now. Maye in the future.

Over time I expect classes to have a tremendous impact on the design of large applications developed with Basic4android.

The beta version is planned to be released in about two weeks...

Two small examples:

1. DraggableView class - wraps an existing view and makes it draggable (similar to the visual designer)
B4X:
'Main activity module
Sub process_globals

End Sub

Sub Globals
   Dim Button1 As Button
   Dim Button2 As Button
   Dim EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   Dim dv1, dv2, dv3 As DraggableView
   dv1.Initialize(Activity, Button1)
   dv2.Initialize(Activity, Button2)
   dv3.Initialize(Activity, EditText1)
End Sub

'***************************
'DraggableView class module
Sub Class_Globals
   Private innerView As View
   Private panel1 As Panel
   Private downx, downy As Int
   Private ACTION_DOWN, ACTION_MOVE, ACTION_UP As Int
End Sub

Sub Initialize(Activity As Activity, v As View)
   innerView = v
   panel1.Initialize("")
   panel1.Color = Colors.Transparent
   Activity.AddView(panel1, v.Left, v.Top, v.Width, v.Height)
   ACTION_DOWN = Activity.ACTION_DOWN
   ACTION_MOVE = Activity.ACTION_MOVE
   ACTION_UP = Activity.ACTION_UP
   Dim r As Reflector
   r.Target = panel1
   r.SetOnTouchListener("Panel1_Touch") 'why reflection instead of the regular Panel_Touch event? Good question which deserves a forum thread of its own (not related to classes)...
End Sub

Private Sub Panel1_Touch (o As Object, ACTION As Int, x As Float, y As Float, motion As Object) As Boolean
   If ACTION = ACTION_DOWN Then
      downx = x
      downy = y
   Else 
      innerView.Left = innerView.Left + x - downx
      innerView.Top = innerView.Top + y - downy      
      panel1.Left = innerView.Left
      panel1.Top = innerView.Top
   End If
   Return True
End Sub

2. Binary tree implementation (sorting algorithm):
B4X:
'Main module
Sub process_globals

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim root As TreeNode
   root.Initialize
   For i = 1 To 1000
      root.AddValue(Rnd(0, 999999))
   Next
   root.Print
End Sub

'*****************************
'TreeNode Class module
Sub Class_Globals
   Private value As Int
   Private left As TreeNode
   Private right As TreeNode
End Sub

Public Sub Initialize
   value = -1
End Sub

Public Sub AddValue(v As Int)
   Dim n As TreeNode
   If value = -1 Then
      value = v
      Return
   Else If v < value Then
      n = left
   Else
      n = right
   End If
   If n.IsInitialized = False Then n.Initialize
   n.AddValue(v)
End Sub

Public Sub Print
   If left.IsInitialized Then left.Print
   Log(value)
   If right.IsInitialized Then right.Print
End Sub
 

Informatix

Expert
Licensed User
Longtime User
So, if i put my class instance into a code module and then instantiated it and set it all up from within the code module, how would the class raise the event and how would the code module receive the event? Can you give me a very simple example please.
A code module cannot receive events (never), and the activity won't receive events raised by a class (e.g. if your class declares a button, the Button_Click event will be received by the class only). Once the event is received, you can dispatch it with a Callsub to the activity or code module, so they can react to it. There's no other way than using a callback function (passed to the class via Initialize).
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Classes are soon coming...

I first thought this is a new thread and Erel is announcing a b4a learning class, so I prepared my notebook and pencils and opend
the thread only to know it is about class :)
But really I see what Klaus and Informatics are contributing is a real online class. I learned here a lot, thanks guys.
 
Upvote 0

Antony Danby

Member
Licensed User
Longtime User
A code module cannot receive events (never), and the activity won't receive events raised by a class (e.g. if your class declares a button, the Button_Click event will be received by the class only). Once the event is received, you can dispatch it with a Callsub to the activity or code module, so they can react to it. There's no other way than using a callback function (passed to the class via Initialize).

Thanks a lot for all your help and sorry for being a doylum ( yorkshire speak for idiot ). I did a bunch of reading; particularly from the end of the user guide and discoverd this whole way of passing Callback As Object, Event As String and then using a CallSub as you say. This method is the most appealling to my Software Engineering side ;-) I suppose equally you could set it and unset it via a seperate method too; not just initialise ( and you could even make a whole sub/pub way of doing it if you stuck the events onto a list? ). If this code is in the class and the calling object is a code module or an activity; then this will work. Is that correct ?

I didn't get round to testing my theory, which is why I ask if it will work; as my computer died before i could test it and i am having to use another PC; not a dev one; to write this. Seriously having a bad day ;-)
 
Last edited:
Upvote 0
Top