Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static variables and initialization

Status
Not open for further replies.

ildenizen

Programmer
Feb 24, 2005
12
US
Ok - It must be late. And yes, I am still a beginner here in the VB stuff.

How can I, within a Private procedure, define a static variable, AND initialize it? In fact, if I am not able to initialize the static item, I am a little confused over why I would ever choose to use a static variable?

All I need is for someone to get my head on straight!
Thanks for your continued patience.
 
Static variables have their uses. There are probable as many uses for them as there are applications.

I use static variables for certain input screens. I'm writing a mapping application, and I have functionality in my software that locates an address on the map. When the user wants to locate an address in my software, they click a button, a modal form is displayed prompting them for the house number, prefix, street name, street type, city, state & zip. Since it's easy for them to mis-type their search criteria... I keep the search criteria in static variable. I don't worry about initialization, and I don't worry about persisting this data after they close the application. However, if the user types 101 N. MANE St, and my application returns... "no matches found", then can click the button and have all their previous search criteria still in the window. They can change MANE to MAIN without having to re-type all the search criteria.

It's a nice little touch that my customers appreciate.

Here's how I use it....

Code:
Private Sub LocateAddress()
  Static HouseNumber as Integer
  Static Prefix As String
  Static StreetName as String
  Static etc....

  call GetAddressSelection(HouseNumber, Prefix, etc....)

  Call GetLatitudeLongitudeForAddress(HouseNumber, Prefix, etc....)

End Sub

Hope this helps.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Static variables in VB6 are the same as instance level variables except that thet are hidden from other procedures. They are not "Shared" by all instances. You could just as well have used...
Private HouseNumber as Integer
Private Prefix As String
Private StreetName as String
Private etc....

Private Sub LocateAddress()

call GetAddressSelection(HouseNumber, Prefix, etc....)

Call GetLatitudeLongitudeForAddress(HouseNumber, Prefix, etc....)



- free online Compare/Diff of snippets
 
Static variables are initialised the same as normal variables, with numeric variables set to 0, string variables set to empty string etc. You can therefore test for that and change if required:
Private Sub Text1_Click()
Static s As Integer
If s = 0 Then s = 6
Text1.FontSize = s
s = s + 1
End Sub


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
When you declare a static within a procedure (sub or function), the value of the static will remain the next time you enter that procedure. the following example illustrates my point. In order to 'see' my point, you must click on the button several times.

Code:
Private Sub Command1_Click()
    
    Static iStaticCount As Integer
    Dim iPrivateCount As Integer
    
    iStaticCount = iStaticCount + 1
    iPrivateCount = iPrivateCount + 1
    
    MsgBox "Static:  " & iStaticCount
    MsgBox "Private:  " & iPrivateCount
    
End Sub

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
And it would also work as
Private iStaticCount As Integer

Private Sub Command1_Click()

Dim iPrivateCount As Integer

iStaticCount = iStaticCount + 1
iPrivateCount = iPrivateCount + 1

MsgBox "Static: " & iStaticCount
MsgBox "Private: " & iPrivateCount

End Sub


- free online Compare/Diff of snippets
 
JohnYingling, the original poster was asking about static variables. I saw his question as 3 parts...

a) How can I, within a Private procedure, define a static variable?
b) How can I initialize it?
c) Why I would ever choose to use a static variable?

My response answered 2 of his questions A and C. johnwm also responded, answering the B question.

Your solution is to make that variable accessible to any and all procedures within that source code module. IMO, it is best to define a variable with as limited a scope as possible. Generally speaking, if you have a module level variable that is ONLY used a a single procedure, you can declare that variable within the procedure as a static variable. It will retain its value within multiple calls to that procedure.

IMO, static variables are not that useful, but should be used when they can be because it limits the scope.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
No offense meant. I wanted to point out that STATIC in VB6 is not the same as Static (Shared) in other languages (VB.Net, C#) where it makes a variable accessible by all instances.

BTW, VB.NET drops support for Static as it is known in VB6.

- free online Compare/Diff of snippets
 
John, thanks for the VB.NET info. I have no plans to upgrade to it any time soon, but there will be a day... Nice to know this before hand. Thanks.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thank you everyone. Yes, I did start this thread, and yes, it does seem as though there are opinions out there on this!

The application I am porting calls a routine. Within the routine, I check a flag/boolean. If the flag is not set, I initialize values and set the flag. The next time the routine is called, since the flag is now set, I instead update some values with my algorithm.
So the flag must persist (ie static). In my prior experience I would have always initialized variables, even if they might likely be initialized by the system that way. Sort of a documentation thing.

So if I hear right, I have two choices here. Forego the nice documentation trail of specifically initializing my variable, letting VB do that for me, or have the calling routine maintain the initialization boolean variable.
Are there other options I am missing?

Thanks!

BTW - in case anyone forgets to say so, this is the best forum I have ever been a part of. Someone always comes to my aid! When I get better at VB, I hope to be able to return the favor!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top