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



muppet wrote:

(sorry if this comes through twice -- my MUA is giving me fits)

On Sunday, June 6, 2004, at 06:32 PM, Daniel Kasak wrote:

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?
eg:

    foreach my $field (@{$self->{fieldlist}}) {
                    my $widget = $self->{form}->get_widget($field);
                # Need to figure out what type of object $widget is
            }


i'm presuming you are trying to write a generic handler for a bunch of widgets in a form. for example, you have an array of widgets which may be entries for strings, or spinbuttons for numbers, or combos for enums, etc.

in that case you want to handle the number, string, and enum types differently.

what aristotle said, that you don't want to differentiate by types, is true; that breaks the ability to use subclasses of things. in this situation, though, polymorphism will require multiple inheritance, which can be a bit much work, so effectively "switch"ing on object type is a decent idea. and, of course, you use perl's "ref" operator to tell into what class an object is blessed.

*however*, in practice, you don't want to know *exactly* what type your object is; you only care if it is descended from the right type. you use "isa" for that:

   foreach my $field (@{$self->{fieldlist}}) {
       my $widget = $self->{form}->get_widget ($field);
       if ($widget->isa ('Gtk2::SpinButton')) {
          # you use spinbuttons for numbers
       } elsif ($widget->isa ('Gtk2::OptionMenu')) {
          # optionmenus are used for the enum stuff
       } elsif ($widget->isa ('Gtk2::Entry')) {
          # it's an entry, so you have a string.
       } else {
          # don't know how to handle it.
       }
   }

Thanks for the suggestions everyone.

Looking back, I probably should have said what I was trying to achieve. I'm basically writing 2 functions to apply a row of a recordset to a form, and to retrieve data from the form and apply the changes back to the DB server ( and navigate through the recordset etc ).

The reason I needed to know the type of widget is just so I know how to get the data, via get_text or whatever.
I'm using the above approach, as it is by far the easiest

You've all probably heard my raving on about this project a bit. Well I'm basically done now, and it works quite well :) I tried using TieScalar but went back to this more 'manual' way as it 'feels' cleaner, and makes undoing changes to the current record easy. It's also easier to handle the more complicated objects like calendars and combo boxes etc.

When I'm done I'll post the whole thing if there's interest. It's probably not much to the seasoned programmers out there, but it sure makes things easier for the likes of an Access / VB guy like me :)

Anyway, thanks for the help!

Dan

--
Daniel Kasak
IT Developer
NUS Consulting Group
Level 5, 77 Pacific Highway
North Sydney, NSW, Australia 2060
T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989
email: dkasak nusconsulting com au
website: http://www.nusconsulting.com.au
Title: CanIt Vote for ID 52080

The following links have been placed here by the NUS Consulting internal spam filter and are for use by NUS Consulting staff only.
Please ingore these links.
Spam
Not spam
Forget previous vote


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