Valhalla Legends Forums Archive | Visual Basic Programming | Static Array

AuthorMessageTime
Lenny
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]
November 26, 2003, 6:38 PM
Spht
[quote author=Lenny link=board=31;threadid=3868;start=0#msg31684 date=1069871923]
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]

Private MyArray()?

Use ReDim to allocate more/less size as needed.
November 26, 2003, 7:11 PM
iago
Keep in mind that ReDim is fairly slow, so don't use it in a loop to increase the size by one every time :)
November 26, 2003, 7:13 PM
Spht
[quote author=iago link=board=31;threadid=3868;start=0#msg31706 date=1069874005]
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]

Why would someone do that?
November 26, 2003, 7:17 PM
Lenny
Well I'm talking about
Static X(0 to aconstant) as String

I've tried redim for this, but doesn't seem to work
November 26, 2003, 7:32 PM
iago
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.
November 26, 2003, 10:23 PM
Stealth
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:
[code]
Static sCache() As String
[/code]

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.
November 26, 2003, 10:24 PM
Lenny
Instead of using a Static array and placing it in a procedure, I should declare a dynamic global array?
November 27, 2003, 2:56 AM
Stealth
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.
November 27, 2003, 6:23 AM
Lenny
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)
November 27, 2003, 6:39 AM
Stealth
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.
November 27, 2003, 7:00 AM
Lenny
Im not quite sure what you mean by making it static and dynamic
November 27, 2003, 7:19 AM
Spht
[quote author=Lenny link=board=31;threadid=3868;start=0#msg31959 date=1069917567]
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)
[/quote]

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:

[code]Private MyArray()

Sub Main()
ReDim MyArray(5) // Allocate 6 slots
End Sub

Sub MyProcedure()
ReDim Preserve MyArray(6) // Allocate an extra slot
End Sub[/code]
November 27, 2003, 4:05 PM
Stealth
As far as my posts are concerned, Static refers to a Static declaration of the variable.
[code]
Static myArray() as String
[/code]
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.
November 27, 2003, 5:28 PM
Puzzle
I don't think a TRUE static array is possible in VB.
November 29, 2003, 2:10 AM
iago
Well, a static String is (dim blah as string * 15), so it would imagine a static array is.

[quote]Is it possible to declare a static array at runtime?
...
It only allows me to declare its size as a constant...[/quote]

It seems like you're trying to make a static array that can change, which doesn't particularely make sense..
November 29, 2003, 4:10 AM
Spht
[quote author=iago link=board=31;threadid=3868;start=15#msg32341 date=1070079054]
[quote]Is it possible to declare a static array at runtime?
...
It only allows me to declare its size as a constant...[/quote]

It seems like you're trying to make a static array that can change, which doesn't particularely make sense..

[/quote]

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.
November 29, 2003, 4:26 PM

Search