|
-
January 17th, 2010, 08:28 AM
#3
Re: need help with cross-thread delagates
In order for MyClassB to be able to call a method form MyClassA, it must have some sort of reference to it.
Make AddLabel() public, and add a member field to MyClassB referencing an instance of MyClassA - objA in your case. You can initialize it in the constructor or using some property/method (making sure it’s done before OnDataRecived() is executed). Then all you need to do is to use the member field to call AddLabel().
Another way would be to use events, where MyClassB would have an event named something like DataRecived, which would hold a delegate for any method that should do something when the event is fired (thus also being able to directly reference an instance method of MyClassA). In this case, MyClassA would most likely be the one responsible for the registration of its method with the event, and it would probably have to hold a reference to MyClassB (or it could have a specialized public registration method that would take a parameter of MyClassB-type). The event’s type (the type of delegates it accepts) would force (better: standardize) the method’s signature, so you can send any data you require in the same manner, to any class.
I’m not saying one way is better than the other, but for what you want to accomplish, try the first way first (somewhat less work, and you’ll probably get a better understanding of how it’s done).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|