PDA

View Full Version : Solved: Execute a sub only one time?



mud2
04-12-2009, 11:00 AM
I've forgotten how! (Old age?): I want to run a sub that defines several values, but only want to run it one time. I need a signal in the sub (or in "common!...Old FORTRAN USER") that will tell the program the sub has been run. Is it "permanent", 'Constant"...or what?

Oorang
04-12-2009, 11:33 AM
Here is one quick and dirty way:
Public Sub Example()
Static blnRunOnce As Boolean
If blnRunOnce Then Exit Sub
blnRunOnce = True
'Do stuff here.
End Sub

By making the variable static, it's value will be preserved between calls. It will be false by default causing it to get flagged as true and the code to execute. In all future calls the value will be True, and it will exit before any code executes.

mud2
04-12-2009, 01:23 PM
"Static"! It came to me just now while showering.

Oorang
04-12-2009, 02:13 PM
You read this thread while showering!?
eek






:jester:

mud2
04-12-2009, 02:55 PM
Sure, I take my work with me!
No, just the memory of "Static" came to me! But your example aso helped!