in reply to Re: Re: "this" variable?
in thread "this" variable?

this just isn't working for me... if I do a $self = shift; in the beginning of that sub function, my $self value is blank. Of course, then, the call
$selection = $comboboxes[$self]->GetString($comboboxes[$self]->Selecte +dItem);
fails. My code currently is this:
sub ComboBox{ $self = shift; #print "In ComboBox - $self"; $selection = $comboboxes[$self]->GetString($comboboxes[$self]->Sel +ectedItem); #print $selection; } foreach my $x (0..$count){ $SUB = $accounts[$x]."ComboBox_Change"; *$SUB = \&ComboBox; }
If those print lines weren't commented out, the output is "In ComboBox - ". Is my logic wrong here, or just my coding? Is there an easier solution I'm overlooking that would still allow for my arrays to grow with the spreadsheet?

Replies are listed 'Best First'.
Re: Re: Re: Re: "this" variable?
by Ovid (Cardinal) on May 30, 2002 at 20:31 UTC

    You need to show us where and how you call the &ComboBox subroutine. Follow Abigail-II's advice and read the appropriate man pages.

    Rather than sit around and try and second guess what you're doing, I figured I could show you what is going on here. For a quick example of OO in Perl and what is going on here, save the following file as 'Counter.pm':

    package Counter; use strict; use Carp; sub new { my ($class,$value) = @_; $value ||= 0; my $self = bless {}, $class; croak "Argument to constructor must be positive integer." if ! $self->_good_value( $value ); $self->{_value} = $value; return $self; } sub value { my $self = shift; $self->{_value} = $_[0] if $self->_good_value($_[0]); return $self->{_value}; } sub increment_value { my $self = shift; $self->{_value}++; } sub decrement_value { my $self = shift; $self->{_value}--; } sub _good_value { my ( $self, $value ) = @_; return 0 if ! defined $value; $value =~ /^\d+$/; } 1;

    And then, in the same directory:

    #!/usr/bin/perl -w use strict; BEGIN { unshift @INC, '.'; # add current directory to include path use Counter; } my $counter = Counter->new; print $counter->value, "\n"; $counter->increment_value; print $counter->value, "\n"; $counter->decrement_value; print $counter->value, "\n"; $counter->value(3); print $counter->value, "\n";

    The above code is rather simple and not how I would really do it, but it gives you an idea of what is going on. If you don't understand it, you really need to learn Perl's OO system, as what I have presented is very basic.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Just to clarify, the OP doesn't control how subroutine is called. When the end user changes the value of the combo box, Windows sends a message to the main window, which in turn dispatches control to the subroutine.
Re: "this" variable?
by Abigail-II (Bishop) on Jun 03, 2002 at 09:08 UTC
    There could be many things wrong with the code (which I cannot comment on because I don't see all of it). But a few points we can see:
    • You are using $self = shift;, and you are not putting a my in front of it. That means you are using a global variable. If you do the same in SelectedItem or GetString you will get problems, as they will overwrite what is in $self.
    • You are using $self as an index into an array. But $self is an object. If you use an object as a number, most of the time, you will get a pretty big number. Perl will happely extend the array @comboboxes for you, but you're likely to run out of memory before Perl has extended the array far enough.

    Abigail