Array Help (Dimensioning)
I have an array that I create in my declarations. Some of my code uses this array when it's not dimensioned. E.G:
VB Code:
Dim array1() as string
Private Sub Form_Load()
Msgbox Ubound(array1)
End Sub
That would give you a "Subscript out of range" because it's not dimensioned yet. Well that's my problem, I need to tell my program to not use my array if it's not dimensioned yet. How would I go about doing this? Thanks.
Re: Array Help (Dimensioning)
Quote:
Originally Posted by Inuyasha1782
... I need to tell my program to not use my array if it's not dimensioned yet. ...
You may (as the simplest) create some error handler but there is a safer (or more usefull if you will) approach - dimention all dynamic arrays on form_load and set some initial value (something like the folowing):
VB Code:
Private Sub Form_Load()
Redim MyArray(0)
MyArray(0) = -99 'if it's a string then set it to "NONE" or something
End sub
Now all you need to do is check UBound is stil = 0 and MyArray(0) is still equal to initial value:
VB Code:
Private Sub Command1_Click()
If UBound(MyArray) = 0 And MyArray(0) = -99 Then
'do something
Else
'do something else
End If
End sub
Re: Array Help (Dimensioning)
you are getting "Subscript out of range" because you have no array set yet. try making it Dim array1(10) as string
Re: Array Help (Dimensioning)
@wiz126:
He knows all that but he also needs to use dynamic array.
Re: Array Help (Dimensioning)
Okay, well I have another similiar confusing problem now.
Here is some coding:
VB Code:
max = UBound(sCookies)
If UBound(Cookies) > 2 Then
max = max + UBound(Cookies)
End If
ReDim Preserve Cookies(max, max, max)
It is defined in the declarations, and set like so:
VB Code:
Private Sub Form_Load()
ReDim Cookies(0, 0, 0)
Cookies(0, 0, 0) = "NONE"
End Sub
For some reason I am getting a "Subscript out of range" on the last line of the first block of code. When I debug I look at "max"'s value and see that it's 8. So there is no negative numbers, why would I be getting this error? Sorry if im being to vague.
Re: Array Help (Dimensioning)
Correct me if I am wrong but I think that you may only redim preserve the final dimension in an array.
Re: Array Help (Dimensioning)
If I didn't preserve it, wouldn't I lose all the information in my array trying to redimension it?
Re: Array Help (Dimensioning)
Quote:
Originally Posted by Inuyasha1782
If I didn't preserve it, wouldn't I lose all the information in my array trying to redimension it?
Yes.
Re: Array Help (Dimensioning)
Quote:
Originally Posted by David.Poundall
Correct me if I am wrong but I think that you may only redim preserve the final dimension in an array.
That is absolutely correct and here is a quote from MSDN:
Quote:
Dynamic Arrays
Only the upper bound of the last dimension in a multidimensional array can be changed when you use the Preserve keyword; if you change any of the other dimensions, or the lower bound of the last dimension, a run-time error occurs.
Re: Array Help (Dimensioning)
Quote:
Originally Posted by Inuyasha1782
If I didn't preserve it, wouldn't I lose all the information in my array trying to redimension it?
You may need to use multiple arrays OR (and I would prefer this method) array of User Defined Type.