Re: How do I retrieve the 'type' of an object

* Daniel Kasak <dkasak nusconsulting com au> [2004-06-07 00:33]:
I'm looping through a set of objects, and I want to get the
type of object, ( eg GtkEntry ) and then decide what to do
based on the type.  How do I do that?

The *correct* way would be not to.

You want to dispatch to the right code/function depending on the
type of object. Notice something? That's what a method call is.

The correct way would be to add this "what to do" as a method to
each of the classes involved. Then you do something like

    foreach my $field (@{$self->{fieldlist}}) {

and that method call it figures out.

This is the central idea of object orientation: instead of
explicitly checking for types, you let the method call dispatch
to the right place implicitly.


I'm not sure how much work would be involved for such an approach
with Gtk2-Perl though, since I've not subclassed/extended
anything yet, only used what was already there.

"If you can't laugh at yourself, you don't take life seriously enough."

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]