Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2007

Tip: Looking for answers? Try searching our database.

What is the 'Object' of these calls?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter Oliphant - 08 Oct 2007 19:43 GMT
I've written a few event handler methods in my time. Most of them are of the
general form:

void EventHandler( Object^ o, EventArgs^ e )
{
}

However, in all of these I've written, and in every example I've ever seen
on MSDN, the "Object^ o" is NEVER used! What exactly is it, and why is it
there if even MS never seems to use it?

Every example I've seen lists it as 'Object^ /*sender*/', indicating it is
the 'sender' (whatever that means), and it's parameter being commented out
means it doesn't seem like it is ever used. Since it is of type Object^ I'm
guessing it must be typecast to be at all useful.

Most sample code, and everything I've ever written just strips this Object
paramater out or ignores it, and nothing seems to go wrong.

What is it for, and when/how is it useful?

[==Peter==]
Cholo Lennon - 08 Oct 2007 20:02 GMT
The Object^ parameter is useful when you handle more than one object with the
same handler. Also it's necessary when the event is raised by an object that is
out of your control (when you don't have a direct controller variable).

Regards

--
Cholo Lennon
Bs.As.
ARG

> I've written a few event handler methods in my time. Most of them are of the
> general form:
[quoted text clipped - 18 lines]
>
> [==Peter==]
Ben Voigt [C++ MVP] - 08 Oct 2007 21:27 GMT
> I've written a few event handler methods in my time. Most of them are of
> the general form:
[quoted text clipped - 16 lines]
>
> What is it for, and when/how is it useful?

Typically, it is the object you subscribed to.  For example:

Button^ b;
b->Clicked += gcnew EventHandler(this, &ClassName::ItWasClicked);

The first argument to ItWasClicked would be b, the Button instance.

Not much use for core logic, but it could be for UI effects.  For example:

void LightMeUp(Object^ sender, EventArgs^ a)
{
   Control^ c = dynamic_cast<Control^>(sender); // C# "as" operator
   if (c != nullptr) {
       c->BackColor = Color::Yellow;
   }
}

EventHandler^ eIn = gcnew EventHandler(this, &ClassName::LightMeUp);
for each (Control^ c in Controls) {
   c->GotFocus += eIn;
   c->MouseEnter += eIn;
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.