newbie question on events and methods...

M

Mark Reed

I am self studying C# and the book I am using states:

Controls have properties that determine appearance, and they also have
events, actions that trigger a response.

It then goes into the next chapter saying controls also have something
called methods, which is something the control "DOES".

I am confused as to the difference between a control event, and a
control method... to me it seems an event is also something the control
"does".

They then go into the show/hide method. What makes this a method rather
than an event? The two areas are confusing to me.

Thanks!
 
N

Nicholas Paldino [.NET/C# MVP]

Mark,

Basically, an event is a notification that is made to anyone listening
when something happens with the object. This allows you to run code when
changes take place in the object.

Methods are actions exposed by the object which can result in the firing
of events. Calling methods needs to be done explicitly, and is not related
to events, except in the sense that more often than not, calling a method
can result in an event being fired.

So, an event is a notification of an action, and a method is an explicit
call to perform an action.

Hope this helps.
 
J

Jako Menkveld

The only real difference in that sense between an event and a method is that
an event if triggered by external things, such as "User click a button",
"Loses focus to another control", "Gets the focus".

The event is raised when this happens, what the control does in response, it
does using a method.

As for show/hide, it is something you do from within the code, not something
the user or the environment does.

Hope this makes sense.

Jako
 
B

Bruce Wood

An event is how an object notifies the "outside world" that something
within the object has happened that the "outside world" might be
interested in.

A method is functionality that an object exposes to the "outside world"
to allow other objects to ask the object to do things.

When some other object calls an object's method, that object may fire
one or more events as a result, notifying "anyone" (any other objects)
listening that the object's state has changed in some way.

Take, for example, the standard .Show() and .Hide() methods of a
System.Windows.Forms.Control.

Every control has several inter-related methods, properties, and events
that have to do with its visibility: a control can either be visible,
or invisible.

If a control is visible, and you call its .Hide() method, the control
will become invisible, its .Visible property will change to false, and
its OnVisibilityChanged event will fire. (Don't ask why Control has
both methods (.Show() and .Hide()) and a property (.Visible) that do
the same thing. I have no idea. It's an odd design.)

If you want to change the visibility of a control, you have to call
either the .Show() or .Hide() method of that control (or set the
Visible property).

However, if you want an object to _react_ to the control appearing or
disappearing, your object should _listen_ to the control's
OnVisibilityChanged event, which will fire whenever the control appears
or disappears.

If you are creating a new kind of Control by inheriting from Control or
one of its descendents, then as a child class object you can use a
simpler method for reacting to the control's visibility change: you can
override the OnVisibilityChanged() method, which the control calls
internally every time it's about to change its visible state... but
that's probably a topic for another post.
 
R

raz

Hi Bruce,

Wow! I have been programming in C# since early betas and have read many
books, but this has to be one of the best explanations I have read so far.

Thanks!
 
M

Mark Reed

indeed... Thanks to everyone for shedding some light into this...

Im still a little cloudy as to methods and events... but its getting
clearer...

Could someone provide a code sample for each and explain what it does that
makes it what it is?

Sorry to be so clueless... coming from a hardware/networking background
where things seemed so black and white.... programming is a frustrating
endeavor for me.

M. Reed
 
B

Bruce Wood

Oh, crap. In the second-to-last paragraph there I should have written
"...listen to the control's VisibilityChanged event,..."

The event is called VisibilityChanged, not OnVisibilityChanged.
OnVisibilityChanged is the protected method for use by child classes,
as the final paragraph explains.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top