Classes are soon coming...

Discussion in 'Android Questions' started by Erel, Jun 4, 2012.

  1. Erel

    Erel Administrator Staff Member Licensed 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)
    '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)
    Dim dv1, dv2, dv3 As DraggableView
    Activity, Button1)
    Activity, Button2)
    Activity, EditText1)
    End Sub

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

    Sub Initialize(Activity As Activity, v As View)
       innerView = v
       panel1.Color = 
    Activity.AddView(panel1, v.Left, v.Top, v.Width, v.Height)
       ACTION_DOWN = 
       ACTION_MOVE = 
       ACTION_UP = 
    Dim r As Reflector
       r.Target = panel1
    "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
          downx = x
          downy = y
          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):
    'Main module
    Sub process_globals

    End Sub

    Sub Globals

    End Sub

    Sub Activity_Create(FirstTime As Boolean)
    Dim root As TreeNode
    For i = 1 To 1000
    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 = -
    End Sub

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

    Public Sub Print
    If left.IsInitialized Then left.Print
    If right.IsInitialized Then right.Print
    End Sub
    fredo likes this.
  2. sioconcept

    sioconcept Active Member Licensed User

    OMG ! It's a real evolution ! Very good Job Staff's B4A
  3. rickrack

    rickrack Member Licensed User

    looks great! :sign0098:
  4. ChrShe

    ChrShe Member Licensed User

    And B4A gets better and better!!!!
    This is going to be fun!!

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

  5. Highwinder

    Highwinder Active Member Licensed User


    Nice job, Erel, this is great news!
  6. birnesoft

    birnesoft Active Member Licensed 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

  7. JoanRPM

    JoanRPM Active Member Licensed User

    Good news!. Thanks Erel.
  8. vb1992

    vb1992 Well-Known Member Licensed User

    From the Message Title, I thought Erel was opening up a b4a school.

    Classes coming soon!

    ( I did too much consulting for an online education company! )

    f0raster0 likes this.
  9. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Well thats important too :D
  10. specci48

    specci48 Well-Known Member Licensed User

    We all are his pupils day by day... :) :sign0027:
  11. margret

    margret Well-Known Member Licensed User

    This will be a great add on!! Looking forward to using it!:D
  12. netchicken

    netchicken Active Member Licensed User

    This will be great. really will be a watershed moment in B4A
  13. Ian Garton

    Ian Garton Member Licensed User

    Wow, this is really going to revolutionise the product.
    Fantastic work Erel.
  14. pluton

    pluton Active Member Licensed User

    I can't wait. Classes will help me alot

    Nice work Erel
  15. JaunLukePicard

    JaunLukePicard Member Licensed User

    Classes in B4A

    Earl, this is definately great news!. I love the software.:sign0098:
  16. COBRASoft

    COBRASoft Active Member Licensed User

    Simply superb!
  17. Ricky D

    Ricky D Well-Known Member Licensed User

    Erel, this sounds like a very good idea. Will I be able to do :

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

    Sub Initialize
    Id = 0
        ShiftDate = 
        ShiftTypeId = 
    End Sub

    'in code elsewhere
    Dim Shift As clsShift

    Shift.Id = 
    Shift.ShiftDate = 
    Shift.ShiftTypeId = 

    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: Jun 6, 2012
  18. warwound

    warwound Expert Licensed User

    So who is gonna volunteer as teacher's pet...??

    There's plenty of us regulars on here to go for it LOL!!!

  19. Erel

    Erel Administrator Staff Member Licensed User


    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:
    Sub SaveAll(ListOfMiscObjects As List)
    For i = 0 to ListOfMiscObjects.Size - 1
    CallSub(ListOfMiscObjects.Get(i), "Save")
    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.
  20. CharlesIPTI

    CharlesIPTI Active Member Licensed User


  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice