Solved

Event handling and communication between two classes

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 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