Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Event handling and communication between two classes

Posted on 2004-08-23
4
Medium Priority
?
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

730 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