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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Arrays

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
I need some advice on using dynamic arrays. Here is my code:
Code:
Dim strHeadingNameArray() As String
  
  If ... Then
    ReDim strArray(1 To 8) As String
    strArray = Array("Item", "Thing", "Misc", "Object", "Box", "Bag", "Pack", "Holdall")
  Else
    ReDim strArray(1 To 7) As String
    strArray = Array("Bell", "Window", "Monitor", "Mouse", "PC", "HDD", "FDD")
  End If

I get an error when I try to assign values to strArray. Does this mean I must assign values individually? Clive [infinity]
 
Coupla points:

1: Why are you dimming an array as string
Arrays (AFAIK) should be dimmed as variants if anything

dim myArray as variant
myArray = array("Bells","Whistles".......

2: Also, I thought that for a single dimensioned array, you only use a single figure ie
dim myArray(8) as variant

which will contain 9 elements 0 - 9 Rgds
~Geoff~
 
Geoff,

Surely it is better to define an array as precisely as possible i.e. if you know the array will only ever contain string values then declare it as a string.

Also,
> dim myArray(8) as variant
>
> which will contain 9 elements 0 - 9
I thought only 8 elements, 0 - 7 would be set up from the above declaration? Clive [infinity]
 
I assume the different spellings of strArray and strHeadingNameArray is an accident of the post...

The simplest way (without "As", strArray type defaults to Variant):

Sub test()
Dim strArray
x = 3
If x = 2 Then
strArray = Array("Item", "Thing", "Misc", "Object", "Box", "Bag", "Pack", "Holdall")
Else
strArray = Array("Bell", "Window", "Monitor", "Mouse", "PC", "HDD", "FDD")
End If
MsgBox (strArray(3))
End Sub
 
D'OH [blush] - rush rush rush (no excuse I know ;-) )
have just read a bit on help about arrays:
Looks like you can use the 1 to 5 syntax etc but it only has this syntax for multidimensional arrays
It says that to change the lower bound value from 0 to 1, you need to set the option base before declaring the array so
Option base 1
Dim StrArray(8) as string (you are correct about that bit)

I agree with Zathras tho - you cannot redim an array that hasn't been declared - I think that's where your problem is....
StrHeadingNameArray
StrArray
...........
HTH Rgds
~Geoff~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top