Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find out when a method is triggered

Status
Not open for further replies.

golcarlad

Programmer
Nov 23, 2004
232
0
0
GB
Hi

I know my problem will invariably involve something to do with delegates and/or events, but I'm not exactly sure how to implement them at the moment.

The basic problem -
1) I must somehow find out when a static method in my class is triggered by some other object.
2) The static method populates a static string variable.
3) An instance method that has listened to the static method realises it has populated the string and will now do soemthing with it - like show a message box or something.

Any ideas guys would be so appreciated.
Cheers!
 
Isn't it as simple as the static method returning the string? Certainly seems so!

If not, you're probably after Observer pattern.

C
 
well, the static method populates the string variable, but when does the instance method know when to show the message box? - it needs to know as soon as the variable is populated and then show the message box.
 
you can most likely do this without static objects. as Craig0201 said, this is a good candidate for the observer pattern.

The observer is the object which will show the message box it's waiting for an update. The notifier is the oject which pushes notifications when it receives an update. the observer will register with the notifier.

A great example of this is GUI events. button clicks, text changes, etc.



Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I appreciate what you guys are saying, but what Im doing is pretty non-standard - it invloves COM classes and .NET wrappers and this causes all kinds of nastiness. The way I see it - I can only use statics, but then again I dont think this will work anyway, I just dont see how an instance method can be notified from static method period!
 
I just dont see how an instance method can be notified from static method period!
that's why we are recommending the observer pattern. I think the problem is the static method is populating a static string. At least in this specific situation. You will not need a static string for this use case. you shouldn't need a static method either COM is unmanaged code. it does require some TLC to work with the CLR, but I doesn't need to be static.

forget the members are static and forget you are using COM, they are clouding the issue. You need a message box to display (and possibly other processes to execute) when a string changes. this is the observer pattern. when something changes notify a group of objects to do something with the updated data.


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
So if my Class does not know anything about the form class - and vice versa, how does the class notify the form that something just happened?

 
if you review the link provided above and click on the 2nd result you will examples of how to apply this.

review the "real world" example provided on this page.
now imagine your objects instead of stocks and investors. IBM would be the COM wrapper object (notifier). Invstors would be the win form (observer).

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top