Is it possible to declare a static array at runtime?
If so can someone show me an example?
It only allows me to declare its size as a constant...
[Visual Basic]
Quote from: Lenny on November 26, 2003, 12:38 PM
Is it possible to declare a static array at runtime?
If so can someone show me an example?
It only allows me to declare its size as a constant...
[Visual Basic]
Private MyArray()?
Use
ReDim to allocate more/less size as needed.
Keep in mind that ReDim is fairly slow, so don't use it in a loop to increase the size by one every time :)
Quote from: iago on November 26, 2003, 01:13 PM
Keep in mind that ReDim is fairly slow, so don't use it in a loop to increase the size by one every time :)
Why would someone do that?
Well I'm talking about
Static X(0 to aconstant) as String
I've tried redim for this, but doesn't seem to work
iirc, you can't redim when you specify a size. Leave the initial size blank.
Spht - I've seen it done. In fact, I've done it before going back, seeing it, and kicking myself.
As Spht said, you can use a module-scope global array in this case which will work just as well as a static array.
I know it's possible to store static arrays:
Static sCache() As String
In this case, it's dynamic; perhaps VB won't let you create static variables with a fixed size? That doesn't make a whole lot of sense, but hey -- it's Microsoft.
Edit: iago, on the contrary -- you can't redim to an empty array, you MUST specify a size. The only way I know of to empty a dynamic array is to ReDim it to 0 and live with an empty base member -- I may be wrong, it's been awhile.
Instead of using a Static array and placing it in a procedure, I should declare a dynamic global array?
From what you've said and I've experimented, if you need to FIX the size of the array, make it module-level global. If you need it dynamic or can use it dynamically, make it static.
Well I need it to be static and also the size be determined at runtime (not hardcoded in)...But it only lets me size it to constants such as (0 to 5) instead of (0 to x)
In that case, make it static and dynamic, then redim it at runtime to whatever size you want. Redimming can be done with non-constant expressions.
Im not quite sure what you mean by making it static and dynamic
Quote from: Lenny on November 27, 2003, 01:19 AM
Well I need it to be static and also the size be determined at runtime (not hardcoded in)...But it only lets me size it to constants such as (0 to 5) instead of (0 to x)
That doesn't exactly make sense unless you have a different meaning of "Static." Are you referring to static as in only being able to reference your array everytime a function is called? Or static as in the value never changes (but you want it to change, so I'll assume you meant the first static). But, you can't change the value at run-time if it's "Static" because only the function which holds the static variable can change it. So you may want to make it global instead. Something like this:
Private MyArray()
Sub Main()
ReDim MyArray(5) // Allocate 6 slots
End Sub
Sub MyProcedure()
ReDim Preserve MyArray(6) // Allocate an extra slot
End Sub
As far as my posts are concerned, Static refers to a Static declaration of the variable.
Static myArray() as String
Dynamic refers to an array whose size is specified or changed during runtime, and Fixed refers to one whose size is specified at design time.
I don't think a TRUE static array is possible in VB.
Well, a static String is (dim blah as string * 15), so it would imagine a static array is.
QuoteIs it possible to declare a static array at runtime?
...
It only allows me to declare its size as a constant...
It seems like you're trying to make a static array that can change, which doesn't particularely make sense..
Quote from: iago on November 28, 2003, 10:10 PM
QuoteIs it possible to declare a static array at runtime?
...
It only allows me to declare its size as a constant...
It seems like you're trying to make a static array that can change, which doesn't particularely make sense..
Which made me think he's referring to the "Static" statement which is declared in a procedure and retains it's value everytime that procedure is referenced.