call event with parameters from another event , both different namespace - c#

I would like to call an event having parameters from another event, both have different namespaces, is it passable to do this?
public void event1(object1 sender, eventArgs e1)
{
// code goes here
}
public void event2(object2 sender, eventArgs e2)
{
// here I want call event1 with parametersenter code here
}
is it possible?

The best way to do this is not in terms of events being called but just methods being called.
If in the simplest terms your events just called Method1 and Method2 respectively then you could easily have Method1 call Method2 and do whatever it does.
You could raise potentially raise event1 from inside event2 but it doesn't necessarily make sense to do so (for example if it is a click event it should only be raised by a click. It is best to just refactor common code into a method that can be easily called using standard OOP techniques. In my code below I've made the extracted methods static methods so they are easier to call from other objects and assumed that Method1 is in Class1 (not explicitly shown) which is in Namespace1. You may need to do things slightly differently if you need them to be non-static methods.
Here's the example of what I mean.
public void event1(object1 sender, eventArgs e1)
{
var myParameter = ...;
Method1(myParameter);
}
public static void Method1(object myParameter)
{
// code goes here
}
public void event2(object2 sender, eventArgs e2)
{
var myParameter = ...;
Method2(myParameter);
}
public static void Method2(object myParameter)
{
Namespace1.Class1.Method1(myParameter);
}

Using "MyNameSpace";
thats all you need.
I need to write 30 characters so
include "mynamespace" for c++.
Unless i misunderstood what you meant.

Related

Can I cast event args inside the called function in C# instead of defining delegates?

I have an event:
public event RoutedEventHandler ActionEvent;
I have a superclass:
internal class MyEventArgs : RoutedEventArgs
{
public enum SomeAction
{
ACTION1,
ACTION2,
ACTION3
}
public MyEvent(SomeAction action)
{
this.action = action;
}
public SomeAction action;
}
I call the function:
private void onAction(object sender, MyEventArgs args) { if (ActionEvent != null) ActionEvent(sender, args); }
And finally, hence my question, I cast inside the called method:
void ActionEvent(object sender, RoutedEventArgs e)
{
MyEvent args = (MyEvent)e;
}
Is this legal? Can I avoid introducing delegates? It compiles and runs happily in debug mode.
(I'm new in C#. I have been using C++ for ages. The answer would be a definite yes in C++, but I want to double check that I can do this in C# as well without introducing "hidden" stability problems, multi-threading problems or anything like that. In fact, C# should warn me in real-time if I used a wrong cast, but just I asked just to double check.)
You can do this - but then you're really being very specific in your implementation, without being specific in the type system.
Why not just use EventHandler<TEventArgs>?
public event EventHandler<MyEventArgs> ActionEvent;
Then:
void ActionEvent(object sender, MyEventArgs e)
{
...
}

Adding event handler before initializier is called

I have my two classes MainWindow and Foo and have a slight problem considering timing:
class MainWindow : Window
{
internal void SomeMethod(string name)
{
Foo foo = new foo(name)
foo.MyEventHandler += EventHandlerMethod;
}
internal void EventHandlerMethod(object sender, EventArgs e)
{
//do something after foo is done initializing stuff
}
}
class Foo
{
internal event EventHandler MyEventHandler;
internal Foo(string name)
{
//start another thread that will at some point via event call FooMethod()
}
private void FooMethod()
{
MyEventHandler(this, null);
}
}
The problem is that I cannot guarantee how long the Foo-initialized thread will take and FooMethod(); might be called before MyEventHandler has been added.
I thought of a possible solution to simply not add the initializer but have a separate method and simply call that one after adding the event, but in general, is there a way to add events BEFORE the initializer is called?
you can't do before, but you can doing it as part of the constructor. Just pass the handler in as a parameter.
However, that's pretty ugly. Having constructors that spawn threads is not nice, much better having a "Start" method

Use of the event keyword in c#

I was wondering what the exact use of events is in c#. I am still in the process of learning c# so I maybe missing something but is it possible to just use delegates.
In this example I wrote a class with a method that counts from 0 to 2^64 and every time it reaches a multiple of a thousand raises an event. Here is the code:
namespace EventDelegate
{
class Program
{
static void Main(string[] args)
{
EventRaiserClass _eventraiser = new EventRaiserClass();
_eventraiser.handler = SomeEventHandler;
_eventraiser.handler += AnotherEventHandler;
_eventraiser.Loop();
Console.Read();
}
static void SomeEventHandler(object sender, EventArgs args)
{
Console.WriteLine("Event raised");
}
static void AnotherEventHandler(object sendr, EventArgs args)
{
Console.WriteLine("Event raised (Another handler)");
}
}
public delegate void Handler(object sender, EventArgs args);
class EventRaiserClass
{
public Handler handler;
public void Loop()
{
for (long i = 0; i < Int64.MaxValue; i++)
{
if ((i % 1000) == 0)
{
EventArgs args = new EventArgs();
RaiseEvent(args);
System.Threading.Thread.Sleep(1000);
}
}
}
private void RaiseEvent(EventArgs args)
{
if (handler != null)
handler(this, args);
}
}
}
What would the difference have been if I had declared the handler delegate variable to be an event like this public event Handler handler.
Sorry if I am been a bit vague or missing something obvious, but I am just wondering if something else happens behind the scenes when using event rather just using delegates or if it's just for readability purposes.
Events and delegates are similar, but events are more restricted, for good reasons.
In your code, you could do all kinds of things with _eventraiser.handler from the outside. You aren't supposed to do most of those things though.
Consider this line:
_eventraiser.handler = SomeEventHandler;
If you use delegates, you would have to check every time you try to attach an event handler if the delegate is null, and then initialize it with =, and if it is not null, you just have to add handlers with +=. If you forget an initialization, you get a null reference exception, if you put in one too many, you will overwrite all the previous things.
If you use events instead of delegates in this example, you don't have to do any of this, and, in fact, you can't even do it. With delegates you could even take it and then pass it around to some other classes, which could potentially be very dangerous.
The same goes for Invoke, and all the other things you can do with a delegate: They aren't there for events. The only things you can do with an event from an outside class is += and -=, that's it. You can view them as delegates with a special public interface with complicated getters and setters.
(Events also have a special add and remove syntax, but that's a rather uncommonly used feature)

Set up single event handler for multiple forms

I have some forms, and in them i have some event functions which are basically identical
I have tried to implement a 'Shared' class and link the Eventhandler to that function, but when i give the function the necessary protection level, it complains about it's non-static-ness and i have to make it static also.
I'm not a fan of static functions, and so ask: Is there a better way to do it?
(In case the above is unclear: I want to do this: Set up single event handler for multiple buttons in .NET? but with multiple forms instead of multiple controls)
EDIT: as per request for more info:
I'm fairly OCD about code duplication, and my program has multiple forms active/hidden at the same time, and obviously i want to close the whole program when the 'x' is pressed so:
class Shared
{
public static void FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
public static void FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Are you sure you want to exit?", "Confirm exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) {
e.Cancel = true;
}
}
}
Very simple functions, i know, but i don't like duplication :P
The above 'configuration' of 'public static' works fine, but i just wondered if there was a 'better way' (tm)
You can use static method and then delegate handling to instance and only then use all prettiness of OOP
public static void GeneralHandler(object sender, EventArgs args)
{
instance.Handle(sender, args);
}
private static MyProcessingClass instance = new MyProcessingClass();
Subscribe like
button1.Event1 += GeneralHandler;
Button1.Event2 += GeneralHandler;
Button1.Event1 += GeneralHandler;
You can further enhance your implementation to support Dependency Injection, like introduce HandlerProvider and encapsulate creating mechanism there, while exposing only interface outside
If you don't want a static class, you have 2 easy options to suit most preferences:
singleton
pass parameter to form ctor
For a singleton:
class EventMangler {
private static readonly _instance = new SomeHandler ();
// although you don't like static methods :(
static EventMangler Instance {
get { return _instance; }
public void SomeEventHandler (object sender, EventArgs e) {
// handle event
}
}
// use EventMangler.Instance
public MyForm () {
InitializeComponent();
button1.Click += EventMangler.Instance.SomeEventHandler;
}
To pass a parameter to the Form's constructor, you have more choices: (a) pass reference to the handler's object, or (b) pass a reference to the handler itself. I prefer option (b) for a single handler. Otherwise, if the parent object - e.g. EventMangler - has multiple handlers, use option (a):
// remove singleton Instance method from EventMangler
// instantiate EventMangler in Program and pass to Form ctors
// pass a single handler reference as Action
public MyForm (Action<object, EventArgs> handler) {
InitializeComponent();
button1.Click += handler;
}

C# Passing Variables Between Methods or Eventhandlers

I am still trying to learn about c#, my question is how would I pass a variable from Item1_Click to Item2_Click, is this the same thing as passing them between methods or is this different because they are event handlers?
public partial class Events : System.Web.UI.UserControl
{
protected void Item1_Click(object sender, EventArgs e)
{
//code
}
protected void Item2_Click(object sender, EventArgs e)
{
//code
}
}
They are still regular methods, so you're still able to call them the same way you normally would.
protected void Item1_Click(object sender, EventArgs e)
{
// Call the event handler for Item2, passing the arguments
// Item1 received
Item2_Click(sender, e);
}
protected void Item2_Click(object sender, EventArgs e)
{
// Make it happen.
}
If you want to re-use Item1_Click just bind the click event of the other object to Item1_Click as well.
See the links below for some more information on events in C#.
MSDN Event Tutorial
MSDN C# Programming Guide (Events)
Event handlers are called by the publisher of the event. So you'd need to cache the value in a member variable if both handlers are in the same type. Item1 click caches something (e.g. the selection in a variable) and Item2 click uses this member variable for its own handling.
However nothing stops you from calling the event-handler#2 from event-handler#1 ; since it is a method after all. In this case, you could slot in the parameter in the EventHandler argument but it is a bit non-intuitive.
What you've shown above, is a method. It's just that I imagine you've subscribed your methods to events on a couple of buttons.
It is then up to the buttons to populate the EventArgs instances themselves. If you wish to alter what goes into an EventArgs then you'd need to inherit from a Button and override the OnClick method to fire the event manually.
You could have some state information on your form (if you want shared information between the two methods). Or if you're literally wanting to pass information from Item1_Click to Item2_click then you can just call:
protected void Item1_Click(object sender, EventArgs e)
{
this.Item2_Click(sender new EventArgs()); // <== Stick information in EventArgs
}
If you want to preserve some value from a first click you can set a variable and read it from your other handler.
public partial class Events : System.Web.UI.UserControl
{
private SomeType variable;
protected void Item1_Click(object sender, EventArgs e)
{
variable = someValue;
//code
}
protected void Item2_Click(object sender, EventArgs e)
{
//code + do stuff with variable
}
}
Event handlers are methods themselves, so no difference there. Sending data (variables if you will) between methods is done through parameters, however, Event handlers are required to have a specific signature, so you can't just add more parameters. The way to go here is to use a class member (field or property) as some sort of "global variable" (global to the class) as mentioned in #Zebi's answer
Hope this helps :)
An event handler is just a method, that is called in some specific scenario. There's nothing to prevent you from explicitlly calling those methods, so
protected void Item1_Click(object sender, EventArgs e)
{
Item2_Click(sender, e); //pass the original arguments
Item2_Click(null, null); //pass some other arguments
Item1_Click(null, null); //recursively call the handler.
}
is perfectly valid C# code. However, it's a bad practice to use event handler for anything else than, basically handling the event. If two handlers need to use some same logic, it's better to do:
protected void Item1_Click(object sender, EventArgs e)
{
CommonLogic();
}
protected void Item2_Click(object sender, EventArgs e)
{
CommonLogic();
}
private void CommonLogic()
{
//the common logic goes here
}
Instead of calling your event handler directly, you might want to create methods that wrap the functionality of Item1_Click and Item2_Click. For example..
public partial class Events : System.Web.UI.UserControl
{
protected void Item1_Click(object sender, EventArgs e)
{
var theParam = GetParamValue();
//code
Item1Method(theParam);
}
protected void Item2_Click(object sender, EventArgs e)
{
Item2Method();
}
private void Item1Method(object param1)
{
// Item 1 code goes here...
}
private void Item2Method()
{
//item 2 code goes here...
// then call Item1Method
var param2 = GetParamValue();
Item1Method(param2);
}
}
This is just an example of how to avoid calling your event handlers. Doing this will make your code more maintainable down the road.
In addition, now you don't have to worry about providing a sender and Event Args as parameters when trying to run the functionality in Item1_Click

Categories