• Welcome to Valhalla Legends Archive.
 

Static Array

Started by Lenny, November 26, 2003, 12:38 PM

Previous topic - Next topic

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]
The Bovine Revolution
Something unimportant

Live Battle.net:

WARNING: The preceding message may have contained content unsuitable for young children.

Spht

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.

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 :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Spht

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?

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
The Bovine Revolution
Something unimportant

Live Battle.net:

WARNING: The preceding message may have contained content unsuitable for young children.

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.  
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Stealth

#6
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.
- Stealth
Author of StealthBot

Lenny

Instead of using a Static array and placing it in a procedure, I should declare a dynamic global array?
The Bovine Revolution
Something unimportant

Live Battle.net:

WARNING: The preceding message may have contained content unsuitable for young children.

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.
- Stealth
Author of StealthBot

Lenny

#9
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)
The Bovine Revolution
Something unimportant

Live Battle.net:

WARNING: The preceding message may have contained content unsuitable for young children.

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.
- Stealth
Author of StealthBot

Lenny

Im not quite sure what you mean by making it static and dynamic
The Bovine Revolution
Something unimportant

Live Battle.net:

WARNING: The preceding message may have contained content unsuitable for young children.

Spht

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

Stealth

#13
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.
- Stealth
Author of StealthBot

Puzzle

I don't think a TRUE static array is possible in VB.