Results 1 to 9 of 9

Thread: 2d Array Rediming, Out of Bounds?

  1. #1

    Thread Starter
    Lively Member xxkmanxx's Avatar
    Join Date
    Jul 2002
    Location
    NJ
    Posts
    66

    2d Array Rediming, Out of Bounds?

    im trying to create an address book program that stores people's names and info. i have a 2d array. but the 2nd time i redim it, i get an out of bounds error. I dont know what's wrong. Here's the code:


    General/Declerations:
    Code:
    Dim Adrs() As String '2d array to store name and info
    Dim AdrC As Integer 'stores the number of people in the array


    Adds name & info array:
    Code:
    Private Sub Command1_Click()
        ReDim Preserve Adrs(AdrC, 11)
    
        Adrs(AdrC, 0) = AName.Text
        Adrs(AdrC, 1) = Address.Text
        Adrs(AdrC, 2) = City.Text
        Adrs(AdrC, 3) = State.Text
        Adrs(AdrC, 4) = Zip.Text
        Adrs(AdrC, 5) = Country.Text
        Adrs(AdrC, 6) = Phone.Text
        Adrs(AdrC, 7) = Work.Text
        Adrs(AdrC, 8) = Cell.Text
        Adrs(AdrC, 9) = Fax.Text
        Adrs(AdrC, 10) = Pager.Text
    
        AddrList.AddItem AName.Text
        AdrC = AdrC + 1
        
        'Clears TextBoxes
        AName.Text = ""
        Address.Text = ""
        City.Text = ""
        State.Text = ""
        Zip.Text = ""
        Country.Text = ""
        Phone.Text = ""
        Work.Text = ""
        Cell.Text = ""
        Fax.Text = ""
        Pager.Text = ""
            
    End Sub

    THANKS

  2. #2
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    read the on-line help on "redim"

  3. #3
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416
    you might need to use 1-11 instead of 0-10

  4. #4
    Hyperactive Member Dmitri K's Avatar
    Join Date
    Sep 2002
    Location
    West Palm Beach, FL
    Posts
    444
    Try to dim the Adrs() with 0, 0 (Dim Adrs(0, 0) As String)

  5. #5

    Thread Starter
    Lively Member xxkmanxx's Avatar
    Join Date
    Jul 2002
    Location
    NJ
    Posts
    66
    i got it workin, jus reversed the dimensions:

    Code:
    Private Sub Command1_Click()
    
        ReDim Preserve Adrs(11, AdrC)
    
        Adrs(0, AdrC) = AName.Text
        Adrs(1, AdrC) = Address.Text
        Adrs(2, AdrC) = City.Text
        Adrs(3, AdrC) = State.Text
        Adrs(4, AdrC) = Zip.Text
        Adrs(5, AdrC) = Country.Text
        Adrs(6, AdrC) = Phone.Text
        Adrs(7, AdrC) = Work.Text
        Adrs(8, AdrC) = Cell.Text
        Adrs(9, AdrC) = Fax.Text
        Adrs(10, AdrC) = Pager.Text
    
    
        AddrList.AddItem AName.Text
        AdrC = AdrC + 1
        'ReDim Preserve Adrs(11, AdrC)
    
        
        AName.Text = ""
        Address.Text = ""
        City.Text = ""
        State.Text = ""
        Zip.Text = ""
        Country.Text = ""
        Phone.Text = ""
        Work.Text = ""
        Cell.Text = ""
        Fax.Text = ""
        Pager.Text = ""
            
    
    End Sub

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104
    In any multi-dimensional dynamic array, you can only redim the last dimension. That's why it worked only when you reversed the arguments.

    To get the effect of redimming an earlier dimension, you will need to make a temporary array, copy the data from the true array to the temporary, dim the true array to the proper size, then move the data back. Moving it back can be the interesting part, since there are different ways it can be accomplished.

    It seems like there was a string several months back that had a better way of doing this, but I forget what it was.

  7. #7

    Thread Starter
    Lively Member xxkmanxx's Avatar
    Join Date
    Jul 2002
    Location
    NJ
    Posts
    66
    someone told me it would be easier to use databases, but i dont know how to do databases? do u think databases would be easier?

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104
    That's a tough question to answer simply. I assume the alternative your using for storage is text files. In my experience, text files are easy to use, and really quick. However, if this gets to any size, a database is definitly easier simply because it will be easier to manage. To use text files, you have to read the whole thing in, and any organization and optimization is up to you. WIth a database, the database engine does much of that for you, and you can write quick SQL queries to get what you want. The downside of the database is that it is much more complex. The complexity isn't overwhelming, and once you get up to speed on it, it's really pretty simple, but there is much more of a learning curve, and there are lots of different options you can play with.

  9. #9

    Thread Starter
    Lively Member xxkmanxx's Avatar
    Join Date
    Jul 2002
    Location
    NJ
    Posts
    66
    so i guess i text file is good for now until i have a need to use databases.

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