is it possible to send event object as a parameter

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

Class CharChecker has a field that is called TestChar that is of type event
CharEventHandler.
I know that if I change in method foo so I have
tester.TestChar +=new CharEventHandler(Testing);
it work so you don't have to mention that.

I have a reason for this testing so it has a value for me.
I wonder if it's possible to send this TestChar that is of type event
CharEventHandler as a parameter to another method
from Main in my example to another method in my case it's foo.

I can understand that what I'm doing seems rather strange but I just want to
know if it's possible.

I think you understand when you look at my code to see what I'm trying to
do.


using System;
public delegate void CharEventHandler(object source, CharEventArgs e);

public class CharEventArgs : EventArgs
{
public char CurrChar;

public CharEventArgs( char CurrChar )
{ this.CurrChar = CurrChar; }
}

public class CharChecker
{
private char curr_char;
public event CharEventHandler TestChar;

public char Curr_Char
{
get { return curr_char; }
set { if (TestChar != null) TestChar(this, new
CharEventArgs(value)); }
}
}

public class MyApp
{
private void foo(CharEventHandler arg, CharChecker tester)
{
tester.arg +=new CharEventHandler(Testing);
tester.Curr_Char = 'T';
}


public static void Main()
{
CharChecker tester = new CharChecker();
foo(tester.TestChar, tester );
}


static void Testing(object source, CharEventArgs e)
{ Console.WriteLine("Testing") }
}

//Tony
 
Tony Johansson said:
Class CharChecker has a field that is called TestChar that is of type event
CharEventHandler.
I know that if I change in method foo so I have
tester.TestChar +=new CharEventHandler(Testing);
it work so you don't have to mention that.

I have a reason for this testing so it has a value for me.
I wonder if it's possible to send this TestChar that is of type event
CharEventHandler as a parameter to another method
from Main in my example to another method in my case it's foo.

No. It's unfortunate that I'm in the middle of writing an article about
the differences between events and delegates - it's not ready to go up
on the website yet.

However, the basic principal is that an event is a pair of methods,
that's all. You can add or remove a delegate via an event, but that's
all it's capable of - it's not a delegate value in itself. C#'s field-
like events declare both an event and a delegate at the same time,
which confuses matters, unfortunately.

What you *can* pass is a reference to a delegate instance. It's not
quite clear what your higher-level goal is, so I don't know whether
using a delegate instead of the event will help you...
 
Back
Top