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
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Will Public variables be able to be used like constants...or will there be another syntax for constants? This could really enhance some functions/subs I use for creating Editbox views. I pass it parameters like min/max, whether it is to be uppercase, data type and action button type (both byte values that would be nice as constants), previous view (Used to set tab/focus order, and Previous View's Next View in its Tag Type is then set to the current control so it goes both ways), etc. I use all this to setup ime options in the sub and store what I need later in a Type inside the Tag. I can then use this in other ime and focus change subs to determine what to do to the view on that event, or for what to give focus to depending on the action button selected. I even store an Action Sub in the Type, so if the action button requires an action it will callsub to the given sub. Really enhances Edit Boxes in B4A. Might even be a nice addition to B4A itself.

Example of the View Functions that would be really good classes:

B4X:
Sub Process_Globals
   Type Control(Name As String, ActionBtn As Byte, ActionSub As String, MinChar As Int, MaxChar As Int, Uppercase As Boolean, PrevView As View, NextView As View)
   Dim Alpha, Numeric, Punctuation As String
End Sub

Sub Init
   Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
   Numeric = "0123456789"
   Punctuation = "!@#$%&*()-+=,.;:?/'" & QUOTE
End Sub

Sub CreateLabel(Text As String, Color As Int) As Label
Dim myControl As Label
Dim ref As Reflector

   myControl.Initialize("")
   myControl.Text = Text
   myControl.TextColor = Color
   myControl.TextSize = 16
   myControl.Typeface = Typeface.DEFAULT_BOLD
   ref.Target = myControl
   ref.RunMethod2("setLines", 1, "java.lang.int")
   ref.RunMethod2("setHorizontallyScrolling", True, "java.lang.boolean")
   ref.RunMethod2("setEllipsize", "MARQUEE", "android.text.TextUtils$TruncateAt")
   ref.RunMethod2("setMarqueeRepeatLimit", -1, "java.lang.int")
   ref.RunMethod2("setSelected", True, "java.lang.boolean")
   Return myControl
End Sub

Sub CreateEdit(Name As String, Text As String, Hint As String, MultiLine As Boolean, DataType As Byte, ActionBtn As Byte, ActionSub As String, MinChar As Int, MaxChar As Int, Uppercase As Boolean, PrevView As View) As EditText
' DataType: 0=Text, 1=Sentence, 2=Alpha, 3=Numeric, 4=AlphaNumeric, 5=Float, 6=Password, 7=Email, 8=URL, 9=Phone, 10=Name, 11=DateTime, 12=Date, 13=Time
' ActionBtn: 0=Next, 1=Done, 2=Previous, 3=Go, 4=Search, 5=Send
Dim ref As Reflector
Dim IME As IME
Dim myControl As EditText
Dim baseOptions As Int
Dim controlBag As Control

   myControl.Initialize("Edit")
   IME.Initialize("IME")
   controlBag.Initialize
   controlBag.ActionBtn = ActionBtn
   controlBag.ActionSub = ActionSub
   controlBag.MaxChar = MaxChar
   controlBag.MinChar = MinChar
   controlBag.Name = Name
   controlBag.PrevView = PrevView
   controlBag.Uppercase = Uppercase
   myControl.Tag = controlBag
   If PrevView Is View Then
      If PrevView.Tag Is Control Then
         controlBag = PrevView.Tag
         controlBag.NextView = myControl
      End If
   End If
   myControl.Text = Text
   myControl.Hint = Hint
   baseOptions = 268435456
   If MultiLine Then
      myControl.SingleLine = False
      myControl.Wrap= True
      baseOptions = Bit.Or(baseOptions, 1073741824)
   Else
      myControl.SingleLine = True
      myControl.Wrap= False
   End If
   Select Case DataType
      Case 0 ' Text
         myControl.InputType = 1
      Case 1 ' Sentence
         myControl.InputType = 16385
         IME.SetCustomFilter(myControl, myControl.InputType, Alpha & Numeric & Punctuation)
      Case 2 ' Alpha
         myControl.InputType = 1
         IME.SetCustomFilter(myControl, myControl.InputType, Alpha)
      Case 3 ' Numeric
         myControl.InputType = 2
         IME.SetCustomFilter(myControl, myControl.InputType, Numeric)
      Case 4 ' AlphaNumeric
         myControl.InputType = 1
         IME.SetCustomFilter(myControl, myControl.InputType, Alpha & Numeric)
      Case 5 ' Float
         myControl.InputType = 12290
         IME.SetCustomFilter(myControl, myControl.InputType, Numeric & "-.")
      Case 6 ' Password
         myControl.InputType = 129
      Case 7 ' Email
         myControl.InputType = 33
      Case 8 ' URL
         myControl.InputType = 17
      Case 9 ' Phone
         myControl.InputType = 3
      Case 10 ' Name
         myControl.InputType = 8289
      Case 11 ' DateTime
         myControl.InputType = 4
      Case 12 ' Date
         myControl.InputType = 20
      Case 13 ' Time
         myControl.InputType = 36
   End Select
   If Uppercase Then myControl.InputType = Bit.Or(myControl.InputType, 4096)
   'flagNoExtractUi= 268435456
   'flagNoEnterAction= 1073741824
   'imeOptions
   Select Case ActionBtn
      Case 0 ' Next 5
         ref.Target = myControl
         ref.RunMethod2("setImeOptions", Bit.Or(baseOptions, 5), "java.lang.int")
      Case 1 ' Done 6
         ref.Target = myControl
         ref.RunMethod2("setImeOptions", Bit.Or(baseOptions, 6), "java.lang.int")
      Case 2 ' Previous 7
         ref.Target = myControl
         ref.RunMethod2("setImeOptions", Bit.Or(baseOptions, 7), "java.lang.int")
      Case 3 ' Go 2
         ref.Target = myControl
         ref.RunMethod2("setImeOptions", Bit.Or(baseOptions, 2), "java.lang.int")
      Case 4 ' Search 3
         ref.Target = myControl
         ref.RunMethod2("setImeOptions", Bit.Or(baseOptions, 3), "java.lang.int")
      Case 5 ' Send 4
         ref.Target = myControl
         ref.RunMethod2("setImeOptions", Bit.Or(baseOptions, 4), "java.lang.int")
   End Select
   IME.AddHandleActionEvent(myControl)
   Return myControl
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Perfect timing, I'm waiting to move house, hopefully it'll be over in a couple of weeks then I want to rewrite a large app I have to 'tabletize' it. I was going to amend my existing code (always dangerous), but with classes will be easier to reorganize a lot of it.

I will be learning and testing as I'm going along, and probably asking plenty of questions, I haven't done a lot of oop, just a bit of Python a while back. A great opportunity to learn.
 
Upvote 0

mac499

New Member
Licensed User
Longtime User
Classes and OOPS

Awesome possibilities,

I've got a complete VB Epos system that's entirely OOP ( even to the point that a till is a collection of objects cashdrawer+receiptprinter+scanner+ touchscreen\keyboard etc.) I can't wait to try and port that lot..
 
Upvote 0

hdtvirl

Active Member
Licensed User
Longtime User
Erel, this is great news for all of us, the ability to use classes at last.

I think we will see an explosion in both new Apps being created with B4A and a new bunch of developers who have vb.net skills that were afraid to touch B4A as it was not an 'OOP' development environment. I too have apps with Classes that I would like to port to B4A. The Java Route is just too painful.

I think a number of Java developers will also jump ship.

Great work and I really look forward to the release of Version 2.0

Regards

BOB
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
What about proper coloring/symbols in the popup list? There was a thread recently asking what the symbols mean- http://www.b4x.com/forum/basic4android-updates-questions/17918-very-basic-question.html

It would be really nice if there was a syntax for constants too and perhaps a restriction on one time assignment, etc. Something like:

dim static final max_char as int (Then Assigning in a Class Constructor only)
or
dim static final int max_char = 10
or
define int max_char = 10
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
With B4A now having 4 module types, can we get some indicators added to the tabs at the top and in the Module list as to the type of Module? Different icons and/or some text in brackets or something saying what the module is at a glance. Might even be good in the sub area on the side too showing the type of sub- Maybe something indicating an Event or Normal Sub, and a minus/plus indicating Private/Public?
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
With B4A now having 4 module types, can we get some indicators added to the tabs at the top and in the Module list as to the type of Module? Different icons and/or some text in brackets or something saying what the module is at a glance. Might even be good in the sub area on the side too showing the type of sub- Maybe something indicating an Event or Normal Sub, and a minus/plus indicating Private/Public?

That's an excellent idea. Also I would like to see the IDE remember the order of the "tabs" when we move them around. In a large project they tend to end up in random order, so it's nice we can move related modules next to each other (especially when working on a few modules at once), but the order is not remembered the next time we open the project.
 
Upvote 0

PHB2

Member
Licensed User
Longtime User
With B4A now having 4 module types, can we get some indicators added to the tabs at the top and in the Module list as to the type of Module? Different icons and/or some text in brackets or something saying what the module is at a glance. Might even be good in the sub area on the side too showing the type of sub- Maybe something indicating an Event or Normal Sub, and a minus/plus indicating Private/Public?

I'll add a +1 to that too. That would be very helpful.
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Ha Ha Ha

What ??

Next you'll want a watch window,

and debugging that actually digs into an object's properties and gives you useful information

C'mon your kidding right..??


:sign0087:

:sign0089:

:)
 
Upvote 0

evilkiwi

Member
Licensed User
Longtime User
How to dock a label

This code is great, drags my labels around the screen nicely. My question is how do I get a label(i'm using labels) to dock at a certain place so it sticks.
 
Upvote 0

Antony Danby

Member
Licensed User
Longtime User
Erel, Can I raise an event from a class, so that I can listen to it, say in my Main module. I don't like CallSub() in a class it goes against years of structured programming. If I can put an event in my main the same was as i can do for say SensorChanged() on PhoneSensors I will be immensly happy. Thanks
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Erel, Can I raise an event from a class, so that I can listen to it, say in my Main module. I don't like CallSub() in a class it goes against years of structured programming. If I can put an event in my main the same was as i can do for say SensorChanged() on PhoneSensors I will be immensly happy. Thanks
Yes you can raise an event from a class but the event will be catched by the class, not by the current activity (Main or any other). The current activity listens to events raised either in the activity code, or in a code module.
 
Upvote 0

Antony Danby

Member
Licensed User
Longtime User
Yes you can raise an event from a class but the event will be catched by the class, not by the current activity (Main or any other). The current activity listens to events raised either in the activity code, or in a code module.

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.
 
Upvote 0
Top