OK, let's try a stupid little example. Let's say I start a new project and
on Window1, I drag out 2 pushbuttons. Now I put the following code into the
action event of PushButton1:
static myCount as integer
myCount= myCount+ 1
me.Caption= str(myCount)
When I push the button, it increments the number shown on the button. I
think that's pretty cool, so I do the same thing in PushButton2. Now I run
my project and press PushButton1 three times, and it clicks up 1, 2, 3. I
press PushButton2 twice and it clicks up 1, 2. I'm pleased with myself (I'm
easily amused), and I want to put a few more buttons out on the window to do
the same thing.
It occurs to me at this point that instead of copying the code from button
to button, that a subclass would be easier and a lot more fun. So I create
a class, call it myButton and make its super PushButton. I copy my code
into the Action event and I'm ready to roll.
I drag a few myButton's out on the window and run the project. I press
myButton1 twice and it clicks up 1, 2. Looking good! So I click myButton2
twice and it clicks up 3, 4. Not at all what I expected!
I fully understand why it works this way, and I approve of it working this
way. I'm just saying it's an easy newbie mistake to make.
Tim
> -----Original Message-----
> From: realbasic-nug-bounces at lists dot realsoftware dot com
> [mailto:realbasic-nug-bounces at lists dot realsoftware dot com]On Behalf Of
> joe at strout dot net
> Sent: Tuesday, February 27, 2007 7:22 PM
> To: realbasic-nug at lists dot realsoftware dot com
> Subject: RE: Confused about new things in RB
>
>
> On Feb 27, 2007, at 23:51 UTC, Tim Hare wrote:
>
> > Let me try to put it another way. The distinction is probably better
> > highlighted by comparing the class vs. the instance. If you were to
> > put the static variable in the Action event of the instance, then it
> > acts in the traditional manner.
>
> I'm not sure what you mean by this. As a static variable, it's a
> single global value that's only accessible from within the method.
>
> > If you use that same static in the
> > class, then it acts slightly differently.
>
> How does it act slightly differently? It's a single global value
> that's only accessible from within the method, no matter where you do
> it. I can see no difference whatsoever.
>
> I'm not trying to pick on you here; I think one of us is confused about
> how static variables work, and if that's the case, there are probably
> others here that are confused too. Seems worth a bit of effort to get
> to the bottom of it.
>
> Best,
> - Joe
>
> --
> Joe Strout -- joe at strout dot net
> Verified Express, LLC "Making the Internet a Better Place"
> http://www.verex.com/
>
> _______________________________________________
> Unsubscribe or switch delivery mode:
> <http://www.realsoftware.com/support/listmanager/>
>
> Search the archives:
> <http://support.realsoftware.com/listarchives/lists.html>
>
> --
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.446 / Virus Database: 268.18.4/703 - Release Date:
> 2/26/2007 2:56 PM
>
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 268.18.4/703 - Release Date: 2/26/2007
2:56 PM
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
|