B4J Question Passing Data back to calling class..

Nokia

Active Member
Licensed User
Longtime User
Need some help on passing data back and forth between open forms on two different classes. I have a class that I call from the Main… when I call the class I call a sub that opens a form. On the form I have a button that calls another class that opens a form so I can select a company from a data grind. But I can’t return values to the class that called it. Says I need to initialize it again.

I am trying to call sub from the class that called the open form and class. If that makes sense..

Here is the call from the main code

B4X:
Sub GenMemKey_Action

  Dim k AsKeyMgr
  k.Initialize
  k.ShowMemberKey(MainForm)

EndSub

Here is the sub that opens

B4X:
PublicSub ShowMemberKey(FormOwner AsForm)

  MemberKeyForm.Initialize("frmMemberkey",322,176)
  MemberKeyForm.SetFormStyle("UTILITY")
  MemberKeyForm.Resizable = False
  MemberKeyForm.BackColor = fx.Colors.LightGray
  MemberKeyForm.RootPane.LoadLayout("frmMemberkey")
  MemberKeyForm.SetOwner(FormOwner)
  MemberKeyForm.Show

EndSub

Here is the code for the button click on frmMemberkey

B4X:
Sub btMmkCompSelect_MouseClicked (EventData AsMouseEvent)

  'getting company
  Dim c As clsCompanySelect
  c.Initialize(Main.strMKDirLoc, Main.strMMDirLoc, MemberKeyForm)
  c.GetCompany(MemberKeyForm, "MemberKeyForm")

EndSub

Here is the code I am running on the ok button click

B4X:
Sub btCSOK_MouseClicked (EventData AsMouseEvent)

  Dim arow(3) AsObject = tblCS.SelectedRowValues

  If strFormName = "MainForm"Then
       Main.CallCompanySelect(arow(0), arow(1), arow(2))
  EndIf
  If strFormName = "MemberKeyForm"Then
        Dim K AsKeyMgr
        K.Initialize
        k.CallCompanySelectMMK(arow(0), arow(1), arow(2))
   EndIf

  cs.close

EndSub

I can pass the data back to a sub Main.CallCompanySelect(arow(0), arow(1), arow(2)) with no issues. when called from the main code. But when I try to pass it back to the class that called the form.. k.CallCompanySelectMMK(arow(0), arow(1), arow(2)) and it does not work.

I haven’t found anything like raising an event .. does anybody have any ideas…
 

Daestrum

Expert
Licensed User
Longtime User
Small example that may help you understand how to return values to a caller. (not the best code but shows the principle of how to do it)

form 1 can call form 2 or form 3 directly
form 2 can call form 3
form 3 will insert "hello" into the textfield1 on the form that called it.
 

Attachments

  • call example.zip
    5.2 KB · Views: 240
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
Small example that may help you understand how to return values to a caller. (not the best code but shows the principle of how to do it)

form 1 can call form 2 or form 3 directly
form 2 can call form 3
form 3 will insert "hello" into the textfield1 on the form that called it.

thanks for the example this is kinda what I was looking for but have one question.. how would I pass back an array if I only have one value to pass back on the callsub2?

B4X:
Dim arow(3) As Object = tblCS.SelectedRowValues
Main.CallCompanySelect(arow(0), arow(1), arow(2))
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Just pass the array back, then in the sub deal with the items within
B4X:
CallSub2(caller,"CallCompanySelect",arow)
...

Sub CallCompanySelect(arow() as object)
  ' do something with arow(0)
  ' do something with arow(1)
  ' do something with arow(2)
End Sub
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
I got it.. thanks for the example.. just what I needed..

B4X:
Sub AsignCompanySelect(data As Object)
   
   Dim arow(3) As Object = data
   
   'kinda like a raised event. setting the values of the company name and ID
   strCompKeyName = arow(0)
   strCompName = arow(1)
   strCompID = arow(2)

   txtCompany.Text = strCompName & " (" &  strCompKeyName & ")"
   
End Sub
 
Upvote 0
Top