views:

295

answers:

3

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...

+3  A: 

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

You would get a reference to your object.

Geoff
+1  A: 

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.

Julien Lebosquain
This is the one thing i'm a bit worried about, I can see that as far as requests and responses go, I will get my object reference back. But you say that the sender parameter is always the object that generated the event. Is this absolute? Or can it be something else (even if not recommended).Because the API I am using allows me to start a monitor passing a user object, that it seems I should get back in the event sender object, so not the object that generated the event itself. Obviously you don't know the specific implementation, but would this be possible do you think?
Adam Cobb
Ah no actually think I know what it is, that is the sender object returned with the response to the start monitor itself, not subsequent events raised. Thanks anyway :)
Adam Cobb
+1  A: 

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.

MusiGenesis