what is static?Code:Static SomeVar as String
Printable View
what is static?Code:Static SomeVar as String
There's a very useful code which reads "F1" :rolleyes: ... do you know it? ;)
Quote:
Static Statement
Used atprocedure level to declarevariables and allocate storage space. Variables declared with the Static statement retain their values as long as the code is running.
Syntax
Static varname[([subscripts])] [As [New] type] [, varname[([subscripts])] [As [New] type]] . . .
The Static statement syntax has these parts:
Part Description
varname Required. Name of the variable; follows standard variable naming conventions.
subscripts Optional. Dimensions of anarray variable; up to 60 multiple dimensions may be declared. The subscriptsargument uses the following syntax:
[lower To] upper [,[lower To] upper] . . .
When not explicitly stated in lower, the lower bound of an array is controlled by the Option Base statement. The lower bound is zero if no Option Base statement is present.
New Optional.Keyword that enables implicit creation of an object. If you use New when declaring theobject variable, a new instance of the object is created on first reference to it, so you don't have to use the Set statement to assign the object reference. The New keyword can't be used to declare variables of any intrinsicdata type and can't be used to declare instances of dependent objects.
type Optional. Data type of the variable; may beByte,Boolean,Integer,Long,Currency,Single,Double,Decimal (not currently supported),Date,String, (for variable-length strings), String * length (for fixed-length strings),Object,Variant, auser-defined type, or anobject type. Use a separate As type clause for each variable being defined.
Remarks
Oncemodule code is running, variables declared with the Staticstatement retain their value until the module is reset or restarted. Inclass modules, variables declared with the Static statement retain their value in each class instance until that instance is destroyed. Inform modules, static variables retain their value until the form is closed. Use the Static statement in nonstaticprocedures to explicitly declare variables that are visible only within the procedure, but whose lifetime is the same as the module in which the procedure is defined.
Use a Static statement within a procedure to declare the data type of a variable that retains its value between procedure calls. For example, the following statement declares a fixed-size array of integers:
Static EmployeeNumber(200) As Integer
The following statement declares a variable for a new instance of a worksheet:
Static X As New Worksheet
If the New keyword isn't used when declaring an object variable, the variable that refers to the object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it doesn't refer to any particular instance of an object. When you use the New keyword in thedeclaration, an instance of the object is created on the first reference to the object.
If you don't specify a data type or object type, and there is no Deftype statement in the module, the variable is Variant by default.
Note The Static statement and the Static keyword are similar, but used for different effects. If you declare a procedure using the Static keyword (as in Static Sub CountSales ()), the storage space for all local variables within the procedure is allocated once, and the value of the variables is preserved for the entire time the program is running. For nonstatic procedures, storage space for variables is allocated each time the procedure is called and released when the procedure is exited. The Static statement is used to declare specific variables within nonstatic procedures to preserve their value for as long as the program is running.
When variables are initialized, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string (""), and a fixed-length string is filled with zeros. Variant variables are initialized toEmpty. Each element of a user-defined type variable is initialized as if it were a separate variable.
Note When you use Static statements within a procedure, put them at the beginning of the procedure with other declarative statements such as Dim.
Static do the same thing as DIM but will take in mind the number. It used it loop when you increment a variable.
Example :
VB Code:
Static i as Byte While i < 100 i = i +1 Msgbox i Wend
I don't think you understand the real usage of static, DaoK....
Yes I understand it... but if you think I do not well bad for you... I do not really care heyhey
I dind't understand either what you mean withor what you said on your last post.Quote:
Static do the same thing as DIM but will take in mind the number
Well, i know what is Static but I post to fast and didnt take time to explain it but I mean than it Declare like Dim but the value in the variable is always saved so you can increment the variable...
You can incremnent the value anyway. Static is only useful if you need your subroutine or function to remember it's previous vaue.
ok thx I didnt know about Function
Maybe this example will help explain the past few posts.
VB Code:
Option Explicit Dim x As Integer Private Sub Command1_Click() Dim y As Integer Static z As Integer x = x + 1 y = y + 1 z = z + 1 MsgBox "X = " & x & vbCrLf & _ "Y = " & y & vbCrLf & _ "Z = " & z End Sub
MarkT have the example about what I told about increment !!! :)
You see than with DIM y still 1 and with static y = 2 :)
Declaring a variable as static inside a procedure or function is about the same as dimming the variable at modular level. It will not lose its value between procedure calls. Since I prefer to keep the scope of variables as narrow as possible, I would declare a variable as static inside a procedure before I would dim it at modular level. If of course more than one procedure were going to need to use this variable, then you would need to dim it a modular level. I have heard some say that static is slower than dim, but I doubt you would notice the difference even if it were.
Ok, but that's because what I post it from MSDN.
I don't know if there's a speed difference... but I would declare it as Static, unless I'm "forced" to declare it as Private (in the declarations of the module)
I always declare as Dim... should I change that to Stactic ?
It looks like Mc Brian would agree with me that it is better to declare something as static inside a procedure than dimming it at a modular level.
humm ok ok, so I might change a lots of project heyhey
UGH! To end the [demeaning word here] fest:
Dim declares a variable. Static declares a variable with a procedure-wide scope. For example, in a Timer, doing this:
Will have i always equal 1 because the variable is destroyed when it goes out of scope by the Timer function returning. However, here:VB Code:
Private Sub Timer1_Timer() Dim i As Long i = i + 1 End Sub
i is incremented and preserved even when the function returns. Do not declare a variable static unless you absolutely need to. For some reason it is considered bad programming practice. Just leave VB to do something like that. :rolleyes:VB Code:
Private Sub Timer1_Timer() Static i As Long i = i + 1 End Sub
Static is used when you want it should retain value even when the sub is finished. In example, In a check box you have to uncheck the check box but you dont want it should run the code again.
for example
VB Code:
Sub Check1_Click() If Check1.Value = 1 then Msgbox "You Cannot Select This feature after selling Product." Check1.Value = 0 End If If Check1.Value = 0 then Msgbox "You have to add Sales tax before unchecking this item." Check1.Value = 1 End if End sub
this code will loop cause after setting the value of check1 it will fire a new Click Event to stop that you use Static.
VB Code:
Sub Check1_Click() if Proccesing = true exit sub Static Proccesing as boolean Proccesing = true If Check1.Value = 1 then Msgbox "You Cannot Select This feature after selling Product." Check1.Value = 0 End If If Check1.Value = 0 then Msgbox "You have to add Sales tax before unchecking this item." Check1.Value = 1 End if End sub