Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Event handling and communication between two classes

Posted on 2004-08-23
4
Medium Priority
?
245 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 750 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

885 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