Understanding Classes (or not)

Bill Norris

Active Member
Licensed User
Longtime User
I have read and re-read the docs on Classes, and for some reason I am just not grasping the concepts of when, why or where to use them. Perhaps somebody can get through to me with some real-world examples or simplified explanationa.
Thanks in advance
 

Azlan

Member
Licensed User
Longtime User
Hi Bill,

A Class in OOP (Google it) parlance is an object, and an object encapsulates methods and properties (although B4A Class does not currently support Properties).

We can then create multiple instances of our class.

A sample class called Customer

B4X:
'Class module
Sub Class_Globals
   
    Public Name As String
    Public Address As String
    Public PhoneNum As String
   
    Private MyVariable as Int ' private variable used only inside class object.
      
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize

End Sub

Public Sub SomeFunction()

' A function/sub that is exposed outside the class object.

End Sub

Private Sub SomeOtherSub

' A Private function/sub that is not exposed outside the class object.

End Sub
Now we can use the customer class as follows
B4X:
'create instance of customer
Dim Customer_A As Customer
Customer_A.Name = "Joe"
Customer_A.Address = "Fifth Avenue"
Customer_A.PhoneNum = "123654789"
Customer_A.SomeFunction ' Calls our object function/sub etc

'create another instance of customer
Dim Customer_B As Customer
Customer_B.Name = "Bob"
Customer_B.Address = "Sixth Avenue"
Customer_BPhoneNum = "987456321"

'Using Initialize Sub these values can be set with that call.

'We can now add the above Customers instances to a list etc.

Dim CustomerList As List : CustomerList.Initialize

CustomerList.Add(Customer_A)
CustomerList.Add(Customer_B)

' Now if we want to get a customer object from the list by the name we can have a function:

Sub GetCustomer(name as String) As Customer
' B4A V2.20 and above features

    For Each c As Customer In CustomerList

        If c.Name = name Then
            Return c
        End If

    Next

End Sub

Dim QueryCust As Customer = GetCustomer("Bob")  'Here QueryCust is another instance of the class.

'Or we could get from the list with the list index etc.



I hope this sheds a bit more light on the subject.

Cheers.
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
I understand what you are saying Azlan but if I put this

B4X:
'Class cMedicine
Sub Class_Globals
   Public Id As Int = 0
   Public MedicineNames As String = ""
   Public BrandName As String = ""
   Public UsedFor As String = ""
   Public Directions As String = ""
   Public Morning As Int = 0
   Public Noon As Int = 0
   Public Evening As Int = 0
   Public Night As Int = 0
   Public AddingNew As Boolean = False
End Sub

then add subs like

B4X:
Public Sub setId(idin as int)
    Id = idin
End Sub

Public Sub getId As Int
    Return Id
End Sub

but of course I do this kind of thing

B4X:
Dim m as cMedicine
m.Initialize
m.Id = 1
m.MedicineNames = "Aspirin 100mg"
m.BrandName = "DBL Aspirin"
.
.
If m.Morning>0 Then
   'process morning calculations
End If

so in a roundabout way B4A does have properties for classes.

regards, Ricky
 
Upvote 0

Azlan

Member
Licensed User
Longtime User
Hi Ricky D,

In the example I gave, the Public variables mimic properties same as your second example.

Properties provide a higher level of encapsulation than public variables.

Although desirable, properties are not high on my wish list for B4A.

Being able to declare classes (public and private) in code for rather than the dedicated class module is desirable.

Cheers.
 
Upvote 0
Top