Hello,
I have an issue with an app in one window with 2 independent classes A and B. From each class I have some instances, stored in 2 arrays in that Window. Now I need that instances from A call instances from B and vice versa including parameters transfer (preferably string). So far I tried handing over addresses of “receiving” functions and delegates but when I want to transfer any variable, the program crashes… Is there a simpler way to call instances of a class and transfer data?
you could give both classes the same interface or exchange data via dictionary.
or the classes using the same base class (super to other class)
if possible a object reference is better.
or encapsulate the data part in a class.
Class A “represents” machines that communicate with the PC through a gateway, which are represented in Class B. Thus, if one of the gateways (instances of Class B) receives new info for a machine, the respective instance of Class A should be informed.
At the moment, I tell Class B the ptr of the receiving method of Class A, when I create a new instance of Class A. Then, for developing purpose, I tell Class B to “inform” the Class A instance through a call from a button. As said, this works as long as I do not include any data.
This is the code in the test method of Class B:
Var sp As New call_a_delegegate(Class_A_receiving_address)
var s as cstring = “JK”
sp.Invoke(s)
When I then step through the code, the program crashes (not the IDE). (Windows 10).
Perhaps you can just give me an example, how to hand over the reference of an instance to an other instance…
How do you acquire “Class_A_receiving_address”? And do you store that as a property of Class B? How are the instances of Class A and Class B related? Do you create them in pairs? If so, do they have identical array indices in the 2 arrays in the window?
it sounds like you need the observer pattern to get the information from one class to the other. Of course, you can make one class the property of another. Which would couple the classes directly. With the observer pattern the classes are coupled more loosely.
Hello Tim,
with your hint it works now so that I can transfer “string”. Thanks.
Just to be sure, I do not “generate” a copy of ClassA instance, it is really just a “pointer”? I did include a property of type ClassA in Class B which I then fill with the acutall instanc of ClassA in this format:
//IFS: Class A, Gateway: ClassB
//Fill reference
Gateways_List(0).Class_B_reference = IFS_List(0)
//Testmethod which calls Class A
Gateways_List(0).SendString
@Beatrix_Willius : I understand your hints (unfortunately) only very general, but if it works out like above, I am already happy
no unfortunately after about 1 second in the called method of Class A the debugger stops and I am back in the source code. I can not see any crash report (or do not know how to get that.)
If you are starting with your app direct coupling is perfectly fine. If your app grows then classes shouldn’t depend directly on each other. The book “Head First Design Patterns” is a timeless classic. The YouTube channel of Xojo has some videos about patterns like the observer.