Android Question [SOLVED] Classes newbie question

makis_best

Well-Known Member
Licensed User
Longtime User
Hi

I have two classes, class A and class B.
Inside class B some variables are filled with data let say Variable A1 and A2
How I can pass the data from A1 and A2 into class A

Thank you.
 

sorex

Expert
Licensed User
Longtime User
you could pass it as variables to a sub call or place the variables in the main code module and use main.varname.
 
Upvote 0

rraswisak

Active Member
Licensed User
Sub from Class_A
B4X:
'Class_A
Sub WhoAreYou(name As String, age As Int)
   Msgbox("My name is " & name & " and i'm " & age & " years old","Hi there...")
End Sub

Sub from Class_B
B4X:
'Class_B
Sub Call_ClassA_Sub
   'initialize the class object and call the sub
   Dim classA As Class_A
   classA.WhoAreYou("John", 20)
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
'Class_A
Public Sub WhoAreYou(name As String, age As Int)
Msgbox("My name is " & name & " and i'm " & age & " years old","Hi there...")
End Sub
I would use always the "scope" (Public, above).

'Class_B
Sub Call_ClassA_Sub
'initialize the class object and call the sub
Dim classA As Class_A
classA.Initialize

classA.WhoAreYou("John", 20)
End Sub
Bad names (Dim objA would be a little bit better) and you forgot to initialize classA (which is an object of type Class_A), you have only declared it.

It's ok.


[to show a MsgBox - better a MsgBoxAsync - the object must be declared in an Activity]
 
Last edited:
Upvote 0

makis_best

Well-Known Member
Licensed User
Longtime User
The only problem is, when initialize class variables dim again and loose the data stored inside
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
then use a global one in your main module which won't be recreated when creating a new class variable.
 
Upvote 0
Top