Android Question [SOLVED] Classes newbie question

makis_best

Active Member
Licensed 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
you could pass it as variables to a sub call or place the variables in the main code module and use main.varname.
 

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
 

LucaMs

Expert
Licensed 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:

makis_best

Active Member
Licensed User
The only problem is, when initialize class variables dim again and loose the data stored inside
 

sorex

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