Solved

Event handling and communication between two classes

Posted on 2004-08-23
4
222 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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