Solved

Event handling and communication between two classes

Posted on 2004-08-23
4
218 Views
Last Modified: 2010-04-15
Hi all,
Ive tried to simplify what my problem is, and its probably straight forward Delegate/Event stuff, but I havent really got the hang of that yet!!

I have two classes, say, class A and class B
Class B has a private member variable called myString = "Hello"

using an Event in Class A, I want to call an EventHandler in class B, which returns the word "Hello" back to Class A.  But how is this done???

0
Comment
Question by:Hob_Nob
  • 2
4 Comments
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11870687
Can't you do this with a property?  If all you need is to return a private member variable, then just create a public property in your B class that returns the string.

Events are more for telling an object they need to do some processing.  It is helpful to think of your object as a real-world-object, for example, a car.  The car would have properties, for example, like color, current speed, engine type, etc.  But it would have events for things such as StartEngine (to initialize the engine "object", and stuff like that), ApplyBrakes, etc.

I hope this is clear.  I suppose what I'm trying to say, is that the Event would take the place of calling a public method.  This is a very simplified view of what events do, but I hope it is a start for you.

-Eric
0
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11870737
I should also mention what delegates ARE, just in case that IS what you want to use.

Delegates are just function pointers... essestially moveable references to methods in your object.  For example, your object B might have an event for, say, StringChanged.  You could then tell your object A to have an EventHandler to trap the StringChanged event in object B.  That way, you could tell your object A to get the new string from B whenever it changes, without having to CHECK all the time if it has changed.

Hope this is more of a clarification.

-Eric
0
 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 250 total points
ID: 11870943
Here is how I would handle the situation that you described using events... I am not sure I would normally solve it this way but not having more information I can not comment on that part...  Note that event args are passed by reference so after triggering an event you can access the properties to use what the other class handling the event put into the properties.  Also, these are just snippets to give you the idea...

in class A:

public delegate void MyEvents( object sender, MyEventArgs mea );
public event MyEvents OnMyEvent;
private MyEventArgs FireMyEvent() {
   MyEventArgs mea = new MyEventArgs();
   if ( OnMyEvent != null ) {
      OnMyEvent( this, mea );
   }
   return mea;
}

public void someFunction() {
   MyEventArgs mea = FireMyEvent();
   System.Console.WriteLine(mea.AString)
   System.Console.ReadLine();
}

class MyEventArgs:

public class MyEventArgs : EventArgs {
   private string aString;
   public string AString {
      get { return aString; }
      set { aString = value; }
   }
}

in class B:

private string myString = "Hello";

public ClassB(ClassA a) {
    // Class B has to know about class A at this point!
   a.OnMyEvent += new MyEvents( a_MyEventHandler );
}

public void a_MyEventHandler( object sender, MyEventArgs mea ) {
   mea.AString = myString;
}
0
 

Author Comment

by:Hob_Nob
ID: 11996580
I got it working based around what NipFriar_Tuck was saying, so thank you Friar Tuck.
Looking back at my problem, I wanted an listener class to await for an event to be fired from a GUI button.

Class A:

ClassB classB = new ClassB();
classB.MyEvent += new EventHandler(fireEvent);

private void fireEvent(object sender, EventArgs e)
{
       MessageBox.Show("Class B just fired an event!!!!!");
}

Class B:

private event EventHandler myEvent;
public event EventHandler MyEvent
{
    add
    {myEvent+=value;}
     remove
    {myEvent-=value;}
}

//Event insantiation:
private void button1_Click_1(object sender, System.EventArgs e)
{  onMyClick(sender, e); }

protected virtual void onMyClick(object sender, EventArgs e)
{
     if(myEvent!=null)
          myEvent(this, EventArgs.Empty);
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now