Results 1 to 10 of 10

Thread: Array Help (Dimensioning)

  1. #1

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    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:
    1. Dim array1() as string
    2. Private Sub Form_Load()
    3.   Msgbox Ubound(array1)
    4. 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.
    Age - 15 ::: Level - Advanced
    If you find my post useful please ::Rate It::


  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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:
    1. Private Sub Form_Load()
    2.     Redim MyArray(0)
    3.     MyArray(0) = -99 'if it's a string then set it to "NONE" or something
    4. 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:
    1. Private Sub Command1_Click()
    2.     If UBound(MyArray) = 0 And MyArray(0) = -99 Then
    3.         'do something
    4.     Else
    5.         'do something else
    6.     End If
    7. End sub

  3. #3
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    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
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

  4. #4

  5. #5

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Array Help (Dimensioning)

    Okay, well I have another similiar confusing problem now.

    Here is some coding:

    VB Code:
    1. max = UBound(sCookies)
    2.     If UBound(Cookies) > 2 Then
    3.         max = max + UBound(Cookies)
    4.     End If
    5.     ReDim Preserve Cookies(max, max, max)

    It is defined in the declarations, and set like so:

    VB Code:
    1. Private Sub Form_Load()
    2.     ReDim Cookies(0, 0, 0)
    3.     Cookies(0, 0, 0) = "NONE"
    4. 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.
    Age - 15 ::: Level - Advanced
    If you find my post useful please ::Rate It::


  6. #6
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    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.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  7. #7

    Thread Starter
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Array Help (Dimensioning)

    If I didn't preserve it, wouldn't I lose all the information in my array trying to redimension it?
    Age - 15 ::: Level - Advanced
    If you find my post useful please ::Rate It::


  8. #8
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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.

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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:
    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.

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width