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)
2. Binary tree implementation (sorting algorithm):
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