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
 

ChrShe

Member
Licensed User
Longtime User
And B4A gets better and better!!!!
This is going to be fun!!

Thanks for all the hard work on this!! :)

~Chris
 
Upvote 0

birnesoft

Active Member
Licensed User
Longtime User
You define a new standart I call Java Basic

Unbelievable how you can make such a good support and at the same Time you write this great program, that makes me able to develop 5 times faster then in Java.

I'm happy to get B4A2.0 soon:sign0060:.

Thank's and good programing

Björn
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Erel, this sounds like a very good idea. Will I be able to do :

B4X:
'class clsShift
Public Id As Int
Public ShiftDate As String 
Public ShiftTypeId As Int

Sub Initialize
    Id = 0
    ShiftDate = ""
    ShiftTypeId = 0
End Sub

'in code elsewhere
Dim Shift As clsShift

Shift.Id = 1
Shift.ShiftDate = "23-06-2012"
Shift.ShiftTypeId = 2
Save(Shift) 

Sub Save(obj As Object) 
    If obj Is clsShift Then
         'code to save a shift
    Else If obj Is clsRoute Then
        'write a route 
     End If
End Sub

My question is will we be able to use code like in the sub Save?

I would want to pass a class to the sub and process them like we can currently do with types.

Regards, Ricky
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
:)

My question is will we be able to use code like in the sub Save?
Yes. Though a "better" OOP design will be to have a Save method in each type of object that is responsible for saving this specific type of object.

You can then do something like:
B4X:
Sub SaveAll(ListOfMiscObjects As List)
 For i = 0 to ListOfMiscObjects.Size - 1
  CallSub(ListOfMiscObjects.Get(i), "Save")
 Next
End Sub

You can also use the new SubExist keyword to check whether the sub exists. This can be useful in cases where you have objects that didn't implement the Save method.
 
Upvote 0
Top