简体   繁体   中英

Understanding C# Events use of sender object

I am reasonably new to C# as a language (coming from a C++ background) and I am currently in the process of writing an application that makes use of an event driven API.

Primarily this consists of registering event/response handlers and starting event monitors then dealing with these asychronous events/responses.

The thing i'm having a bit of trouble understanding is the use of the sender object.

What I would like to use it for is to pass a handle to a class object I have with various structures and data in when making a request (or setting up a monitor). And then on the response being recieved/the event being raised I can take the sender object, cast it back to the expected class type and access members, make further changes etc. so treating it as if it's just still a pointer to the original data (which I'm hoping it would be?).

So my question really is, as I am passing a class object in my request, will this be effectively a reference, or will it be copied somewhere along the line by value as it is actually just a generic object and I will end up with an empty copy of my class object on the event?

Or the third option that i'm maybe completely on the wrong track here and should forget the whole thing? :)

Problem is my brains still working in pointers mode I think...

It's a reference. Try out this code to see how it works:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    Whatever(sender);
}

private void Whatever(object sender)
{
    TextBox tb = (TextBox)sender;
    tb.Text = "yo";
}

If object wasn't passed by reference, textBox1 would retain whatever text you typed into it.

I don't know that I entirely understand your question. But to answer you in part:

You would get a reference to your object.

In .NET, all classes are reference types, and a reference is always passed... by reference (the current implementation of a reference is a pointer that can be moved around by the GC when it needs to), so you don't have to worry about anything.

About events, the sender parameter is always the object that generated the event (for example a Button in a Click event on a button). If you want to pass arbitrary data in a custom event, inherits from EventArgs and pass it as the second argument.

Just a note: If you have multiple different controls leading to the same method you can use

((Control)sender)

to access it for every control, regardless the type of control (above it was just hardwritten, what object it has to be)

The good thing about using this technique is that is even applicable on other platforms such as MonoDevelop using mono. Example of this would be :

protected void GetDate(object sender, EventArgs e)
{
     Gtk.Calendar calendar = (Gtk.Calendar)sender;
     HomeLabel.Text = calendar.Date.ToString(); 
}

When parameters are passed by reference,

1. The properties present inside the reference instance can be change without affecting the original reference instance.

2. Original reference can be changed using the ref keyword.

The following is an example

public partial class ParentForm : Form
{
    public ParentForm()
    {
        InitializeComponent();
        ChildForm childForm = new ChildForm();
        ChangeCaption( childForm );
        //Will display the dialog with the changed caption.
        childForm.ShowDialog();
        ChangeCaption( ref childForm );
        //Will give error as it is null
        childForm.ShowDialog();
    }

    void ChangeCaption( ChildForm formParam )
    {
        // This will change openForm’s display text
        formParam.Text = "From name has now been changed";
        // No effect on openForm
        formParam = null;                        
    }


    void ChangeCaption( ref ChildForm formParam )
    {
        // This will change openForm’s display text
        formParam.Text = "From name has now been changed";
        // This will destroy the openForm variable!
        formParam = null;                        
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM