B4J Question How to use a class in a class definition

Raf61

New Member
Hello! I want to make a class, where the class is a parameter in the sub. Is this possible?

Something like this:

Class Tupel:
Sub Class_Globals
    Public x As Float
    Public y As Float
    
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(pX As Float, pY As Float)
    x = pX
    y = pY
End Sub

Public Sub Add(pXY As Tupel)
    x = x + pXY.x
    y = y + pXY.y
End Sub

Thx for your help!
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Some additional suggestions:
1. Keep class globals private as much as possible
2. Return Me as ... in Initialized so that the objects can be return from Initialize
3. For general functions such as add, create and return a new object
4. To save on many Initialize statements, keep initialize simple and make a creator instead

See code below:
B4X:
Sub Class_Globals
    Private x As Float
    Private y As Float
End Sub

Public Sub Initialize As Tuple
    Return Me
End Sub

Public Sub Add(pXY As Tuple) As Tuple
    Dim result As Tuple
    result.Initialize
    result.x = x + pXY.x
    result.y = y + pXY.y
    Return result
End Sub


Public Sub Subtract(pXY As Tuple) As Tuple
    Dim result As Tuple
    result.Initialize
    result.x = x - pXY.x
    result.y = y - pXY.y
    Return result
End Sub

'This is a very useful tuplet creator - avoids many 'Initialize' lines
Public Sub XY(pX As Float, pY As Float) As Tuple
    Dim result As Tuple
    result.Initialize
    result.x = pX
    result.y = pY
    Return result
End Sub

To use...

B4X:
Sub Class_Globals
    Private Root As B4XView    'ignore
    Private xui As XUI   
    Private pz As Tuple
End Sub

Public Sub Initialize
    pz.Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    someSub
End Sub

Sub someSub
    Dim p0 As Tuple = pz.XY(100, 5)
    Dim p1 As Tuple = pz.XY(-100, -5)
    Dim sum As Tuple = p0.Add(p1)
    Dim dif As Tuple = p0.Subtract(p1)
    Dim more As Tuple = p0.Add(pz.XY(25, 35)).Subtract(pz.XY(5, 5))
    Log(p0)    'x=100.0, y=5.0
    Log(p1)    'x=-100.0, y=-5.0
    Log(sum)    'x=0.0, y=0.0
    Log(dif)    'x=200.0, y=10.0
    Log(more)    'x=120.0, y=35.0
End Sub
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
If you add this sub to the class then you can use .show to log the global contents

B4X:
'This is a simple sub to log the contents of global variables of any class
Public Sub show As Tuple
    Dim str As String = Me
    Log(str.SubString2(1, str.IndexOf(", main")))
    Return Me
End Sub

Then someSub becomes...

B4X:
Sub someSub
    Dim p0 As Tuple = pz.XY(100, 5).show
    Dim p1 As Tuple = pz.XY(-100, -5).show
    Dim sum As Tuple = p0.Add(p1)
    Dim dif As Tuple = p0.Subtract(p1)
    Dim more As Tuple = p0.Add(pz.XY(25, 35)).Subtract(pz.XY(5, 5))
    sum.show
    dif.show
    more.show
   
'    x=100.0, y=5.0
'    x=-100.0, y=-5.0
'    x=0.0, y=0.0
'    x=200.0, y=10.0
'    x=120.0, y=35.0
End Sub
 
Last edited:
Upvote 0
Top