Re: Lots of static variables

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Rick Rothstein (rickNOSPAMnews_at_NOSPAMcomcast.net)
Date: 02/26/04


Date: Thu, 26 Feb 2004 08:47:31 -0500


> I've recently started maintaining some VB code, and the thing that most
> suprises me is how virtually all variables are declared static. Variables
> with a clearly local meaning in a Sub or Function are declared Static,
> sometimes even including the 'i' in a loop.
>
> I understand the next time I call the procedure the value of static
> variables will be preserved, but most of these variables are explicitly
> initialized every time the function executes anyway. The cases where I've
> found the variable to be semantically static (there is some reason to
> preserve its value) are very rare.
>
> Is there some reason for this? Efficiency? Or is it just plain wrong?

It would be plain wrong... Static variables are somewhat slow as I recall,
so I would change the Static to Dim for the cases you described. However, be
careful regarding those that look initialized. If you have something like
this

     If YourStaticVariable = 0 Then
       YourStaticVariable = <some value other than zero>
     End If

That is a legitimate way of giving a Static variable its "first" value
(assuming it is never supposed to be zero) and will only be executed the
first time the Sub or Function is called. Same with a String variable and
the Empty String ("").

Rick - MVP



Relevant Pages

  • Re: Lots of static variables
    ... I'd say it's plain wrong. ... Robert ... > initialized every time the function executes anyway. ... > found the variable to be semantically static (there is some reason to ...
    (microsoft.public.vb.general.discussion)
  • A Jew with a Tattoo
    ... I only got Ginsberg's "Howl" the first time for Christmas 2008. ... be be true, yet for whatever reason, they apparently can be. ... The only human beings whose opinions about God, or fellow human beings, ...
    (soc.culture.jewish)
  • Re: Affecting Institutional Change (Yeah Right)
    ... asking for a copy in plain text? ... despite giving them a nice explanation the first time ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
    (Debian-User)
  • Re: Motorcycle Maintenance Photos
    ... The exercise is called "steep turns", and you have to do a 360 degree circle with a 45-degree bank and within 100 feet of altitude and also with some airspeed tolerance. ... Second reason: sometimes you can intersect your own wake turbulence near the end, which terrified me the first time I did it. ... First time I hit my own wake the whole plane shook and I just about peed my pants and I said to the flight instructor "What just happened?". ... by the time you're 20 feet off you should already being applying a mild correction. ...
    (rec.motorcycles)
  • Ping: Mr Chaplin - OT: speaking of runners
    ... a washout for some or other reason (high fuel consumption and frequent ... was like that so that the Luftwaffe could not see the lights on the ... lights on the dashboard - it was a very basic vehicle, slow noisy, ... Doing a job RIGHT the first time gets the job done. ...
    (sci.military.naval)