dotnetluke

I need to create an event class that will:

The event will fire in one class...and a different class will consume the event.

public class Event

{

public delegate void MyHandler();

public event MyHandler MyEvent;

public void FireEvent

{

MyEvent();

}

}

How to I subscribe to the event class in one class...and fire FireEvent event from another Thanks



Re: Visual C# General Event best practice

TaylorMichaelL

You should never raise an event on a class from another class. Provide a method to do it instead. Here is the general layout (using standard event handler and arguments, you can create a custom one as needed).

Code Snippet

public class RaiseEventClass
{

public event EventHandler MyEvent;

protected virtual void OnRaiseEvent ( )

{

EventHandler hdlr = MyEvent;

if (hdlr != null)

hdlr(this, EventArgs.Empty);

}

public void DoSomethingToRaiseEvent ( )

{

OnRaiseEvent();

}
}

public class SubscribeToEventClass

{

//Doesn't really matter how you get access to the class

private RaiseEventClass m_Class = new RaiseEventClass();

public SubscribeToEventClass ( )

{

m_Class.MyEvent += OnMyEvent;

}

public void CauseEventToBeRaised ( )

{

m_Class.DoSomethingToRaiseEvent();

}

private void OnMyEvent ( object sender, EventArgs e )

{

//Do something

}

}

Michael Taylor - 4/16/07

http://p3net.mvps.org





Re: Visual C# General Event best practice

dotnetluke

Trying to comple...

Error 1 'WindowsApplication1.RaiseEventClass.OnRaiseEvent()': no suitable method found to override C:\Documents and Settings\cable\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Class1.cs 11 33 WindowsApplication1





Re: Visual C# General Event best practice

TaylorMichaelL

Fixed code.



Re: Visual C# General Event best practice

dotnetluke

still getting:

Error 1 No overload for 'OnMyEvent' matches delegate 'System.EventHandler' C:\Documents and Settings\cable\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Class1.cs 45 32 WindowsApplication1





Re: Visual C# General Event best practice

TaylorMichaelL

Third time's a charm...



Re: Visual C# General Event best practice

dotnetluke

Thanks! that sample code has shown me more than 4 hours of event posts elsewhere.

(looks like Dr West in action)

Thanks again.





Re: Visual C# General Event best practice

dotnetluke

You would'nt happen to have time to add a ...string eventarg to that would ya

You would'nt believe how much easier it is to learn from an example thats "speaks english"....





Re: Visual C# General Event best practice

TaylorMichaelL

As soon as you start adding parameters then it gets a little harder because you need to add an event args class and a new delegate. But the code is straightforward.

Code Snippet

public sealed class SomethingEventArgs : EventArgs
{

internal SomethingEventArgs ( string parameter )

{ m_Parameter = parameter; }

public string Parameter
{

get { return m_Parameter; }

}

//Just for consistency, not technically required

public static MyEventEventArgs Empty = new MyEventEventArgs("");

private string m_Parameter;

}

public delegate void SomethingEventHandler ( object sender, SomethingEventArgs e );

//In a class...

public event SomethingEventHandler Something;

//Alternative: public event EventHandler<SomethingEventArgs> Something; //Not as readable to me

protected virtual void OnSomething ( string parameter )

{

SomethingEventHandler hdlr = Something;

if (hdlr != null)

{

hdlr(this, new SomethingEventArgs(parameter));

}

}

Notice that the event args are not directly creatable. For most events this is reasonable but some you might want to make public. However in almost all cases the parameters are not writable so you have to pass everything into a constructor. Exceptions exist. If you want to write a pre-event (one that is raised before some action occurs) that can cancel the actual operation then derive from CancelEventArgs instead.

Michael Taylor - 4/16/07

http://p3net.mvps.org





Re: Visual C# General Event best practice

dotnetluke

Still having a little trouble with this...

I have a usercontrol that naigates to a web page.

In the control I need to recieve an event from another class...(instance of RaiseEvent from yet another class (will eventually return the url I need Smile

So the control subscribes....

SubscribeToEventClass subscribe = new SubscribeToEventClass();

There is no way to wire to the event that will be fired in the RaiseEvent instance.

All I can do is:

subscribe.CauseEventToBeRaised// This seems it should be in the RaiseEvent class





Re: Visual C# General Event best practice

TaylorMichaelL

If you are talking about a user control in an ASP.NET page then eventing won't really work properly because the instances only last as long as the request takes to process.

In the example I gave I used the SubscribeToEventClass only as an example. You could put this same code into the user control. The code to subscribe to an event SomeEvent from some instance of a class SomeClass is:

SomeClass.SomeEvent += SomeEventHandler

SomeEventHandler is a method in the calling class that will handle the event. To raise the event from the user control itself you'll have to invoke a method that you create on the SomeClass class.

Michael Taylor - 4/16/07

http://p3net.mvps.org





Re: Visual C# General Event best practice

dotnetluke

Ok...I'll chew on that for awhile. The usercontrol is a bandobject that already has the current instance of IE....so its handy to use an event from the other class (Dictionary of Labels(for the UI).... and associated Urls)...to just re-naviagte from the bandobject when the event is recieved.

Thanks again.