LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default A rude awakening to STATIC variable


I have just learnt (from serendipity) that if a variable is declared
global - PUBLIC either by way of a direct PUBLIC declaration or using
a DIM statement at module level, the variable so treated becomes
"static". Namely that, it exits the procedure it is worked upon with a
runaway value which is accessible by other procedures in the module.

The cut of my rude wakening is that, at any later stage (while the VBE
is open), if the "host procedure" is fired again, the "entry value" of
the variable takes on the last "exit value" . To illustrate,
supposing we have:

Dim x as Integer

Sub ArithSeries()
For i = 1 To 10
x = x + i
Next
MsgBox x
End Sub

The first run of the code will yield x=55, the second x=110, the third
x=165 etc. , the same way we would expect a "Static x as Integer"
local declaration behave ( with of course the one crucial difference
that the global integrity hold sway in the one and not the other).


While the foregoing characteristic of a global declaration can be
exploited for a purposeful end, it could be tamed to operate normally
without the "static" overlay by simply intializing the variable to 0 (
or empty string as the case may ) just prior to looping. The
normailzed version is:

Dim x as Integer

Sub ArithSeries()

x = 0
For i = 1 To 10
x = x + i
Next
MsgBox x
End Sub

This way, every run will faithfully produce a global x=55 result.


--
davidm
------------------------------------------------------------------------
davidm's Profile: http://www.excelforum.com/member.php...o&userid=20645
View this thread: http://www.excelforum.com/showthread...hreadid=394317

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenating two variable fields into a static length text field Young Programmer Lad Excel Worksheet Functions 2 April 17th 09 05:39 PM
STATIC VARIABLE NOT AVAILABLE IN ANOTHER MODULE CAPTGNVR Excel Discussion (Misc queries) 2 February 1st 07 07:30 PM
Static variable cottage6 Excel Programming 0 June 24th 05 07:52 PM
NOT static variable problem Neal Zimm Excel Programming 4 April 22nd 05 04:38 PM
Static Variable Todd Huttenstine Excel Programming 7 May 7th 04 11:16 AM


All times are GMT +1. The time now is 09:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"