Results 1 to 24 of 24

Thread: Multidimensional array

  1. #1

    Thread Starter
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650

    Multidimensional array

    i want to redclare a multidimensional array, but it tells me that the subscript is out of range. im using redim preserve, heres the code:

    VB Code:
    1. Public Map() As Byte
    2. Public Sub LoadMap(MapPath As String)
    3. Dim Line, Token As String
    4. Dim HCounter, VCounter As Integer
    5. Open App.Path & "\maps\" & MapPath & ".map" For Input As #1
    6. Do Until EOF(1)
    7. VCounter = VCounter + 1
    8. Line Input #1, Line
    9. Do While HCounter <= Len(Line)
    10. HCounter = HCounter + 1
    11. ReDim Preserve Map(HCounter, VCounter)
    12. Token = Mid(Line, HCounter, 1)
    13. If Token > 0 Then
    14. Map(HCounter, VCounter) = 0
    15. Else
    16. Map(HCounter, VCounter) = 1
    17. End If
    18. Select Case Token
    19. Case "0"
    20. frmScreen.Picture = LoadPicture(App.Path & "\graphics\grass.gif")
    21. Case "1"
    22. frmScreen.Picture = LoadPicture(App.Path & "\graphics\water.gif")
    23. End Select
    24. BitBlt frmScreen.picGame.hDC, (HCounter - 1) * 40, (VCounter - 1) * 40, 40, 40, frmScreen.hDC, 0, 0, SRCCOPY
    25. Loop
    26. Loop
    27. End Sub

    The code is in a module. Does anyone know whats wrong?

  2. #2
    Lively Member technitaes's Avatar
    Join Date
    Oct 2002
    Location
    Tennessee, USA
    Posts
    112
    ReDim Preserve only allows you to change the size of the last subscript of an array. In your example you can only change the size of vCounter.
    tecnithV -- artisan, artificer, workman, mechanic, architect, or builder
    VB6, ASP, HTML, XML, Oracle, Access, MySql, PHP, C++, Etc...
    To format your VB code in this forum use tags like this [vbcode]your code here[/vbcode]

  3. #3

    Thread Starter
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    what should i use insted then?

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    use collections

  5. #5
    Hyperactive Member Blinky Bill's Avatar
    Join Date
    Mar 2002
    Location
    Happily munching on the greenery in your garden
    Posts
    349
    Use 2 arrays in a class.
    We don't know what's wrong. . . So the best bet might be to remove something surgically.

  6. #6

    Thread Starter
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    two arrays wont work, that would layout the map like this:

    0
    0
    0
    0

    i need this:

    0000000000000
    0000000000000
    0000000000000

    how do collections work? what are collections?

  7. #7
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by Mushroom Realm
    two arrays wont work, that would layout the map like this:

    0
    0
    0
    0

    i need this:

    0000000000000
    0000000000000
    0000000000000

    how do collections work? what are collections?
    It won't if you know how to implement it.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  8. #8
    Hyperactive Member Blinky Bill's Avatar
    Join Date
    Mar 2002
    Location
    Happily munching on the greenery in your garden
    Posts
    349
    2 arrays and 2 indexs can work the same way as a single multi dimensional array. Define one for x and another for y and bingo! Use a class to define functions to manipulate the arrays.
    We don't know what's wrong. . . So the best bet might be to remove something surgically.

  9. #9

    Thread Starter
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    what wont? when i typed the two array part i meant it to be diagonal, something must have changed it.

  10. #10
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by Mushroom Realm
    what wont? when i typed the two array part i meant it to be diagonal, something must have changed it.
    diagonal? What do you mean?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  11. #11

    Thread Starter
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    i should say maybe like this:

    0000000000000000000
    01
    0 1
    0 1
    0 1
    0
    0
    0
    0

    the zeros r the actual arrays, the 1's represent a visual image as if in x,y form

  12. #12
    Hyperactive Member Blinky Bill's Avatar
    Join Date
    Mar 2002
    Location
    Happily munching on the greenery in your garden
    Posts
    349
    A diagonal array is very, very strange. What do you mean? And why do you want a diagonal array? How does that differ from a single dimensional array?
    We don't know what's wrong. . . So the best bet might be to remove something surgically.

  13. #13
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I don't understand what you mean... but for example:
    VB Code:
    1. Dim Position(5, 5) As Integer
    would be the same as:
    VB Code:
    1. Dim x(5) As Integer, y(5) As Integer
    It's just a matter on how you code it later...
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  14. #14
    Hyperactive Member Blinky Bill's Avatar
    Join Date
    Mar 2002
    Location
    Happily munching on the greenery in your garden
    Posts
    349
    Okay, i get what you mean now. But how will 2 arrays not implement this?
    We don't know what's wrong. . . So the best bet might be to remove something surgically.

  15. #15

    Thread Starter
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    the first way gives you 5*5 variables, the second way (two arrays) only gives you 10

  16. #16
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by Mushroom Realm
    the first way gives you 5*5 variables, the second way (two arrays) only gives you 10
    No!! It will give 25 as well
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  17. #17
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    x(0), y(0)
    x(0), y(1)
    x(0), y(2)
    x(0), y(3)
    x(0), y(4)

    x(1), y(0)
    x(1), y(1)
    x(1), y(2)
    x(1), y(3)
    x(1), y(4)

    x(2), y(0)
    x(2), y(1)
    x(2), y(2)
    x(2), y(3)
    x(2), y(4)

    x(3), y(0)
    x(3), y(1)
    x(3), y(2)
    x(3), y(3)
    x(3), y(4)

    x(4), y(0)
    x(4), y(1)
    x(4), y(2)
    x(4), y(3)
    x(4), y(4)

    There you have the 25 combinations....
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  18. #18

    Thread Starter
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    but im only storing one byte, so u cant merge the x and the y, and two arrays would also require that example: the value for the x(2) be the same in every line (y), (x(2) used as example)

  19. #19
    Hyperactive Member Blinky Bill's Avatar
    Join Date
    Mar 2002
    Location
    Happily munching on the greenery in your garden
    Posts
    349
    Wrong. If you code it right you get 25 with 2 arrays

    1 2 3 4 5
    1
    2
    3
    4
    5
    We don't know what's wrong. . . So the best bet might be to remove something surgically.

  20. #20
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by Mushroom Realm
    so u cant merge the x and the y
    What do you mean?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  21. #21

    Thread Starter
    Fanatic Member Mushroom Realm's Avatar
    Join Date
    Mar 2002
    Location
    Murrieta, California
    Posts
    650
    u have x(0),y(0) x(0),y(1) im assuming u meant to merge the valuse of x(0),Y(0) or whatever so that u could have 25

  22. #22
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    Originally posted by Mc Brain
    x(0), y(0)
    x(0), y(1)
    x(0), y(2)
    x(0), y(3)
    x(0), y(4)

    x(1), y(0)
    x(1), y(1)
    x(1), y(2)
    x(1), y(3)
    x(1), y(4)

    x(2), y(0)
    x(2), y(1)
    x(2), y(2)
    x(2), y(3)
    x(2), y(4)

    x(3), y(0)
    x(3), y(1)
    x(3), y(2)
    x(3), y(3)
    x(3), y(4)

    x(4), y(0)
    x(4), y(1)
    x(4), y(2)
    x(4), y(3)
    x(4), y(4)

    There you have the 25 combinations....
    but you can't store 25 info like that, you could only store 5, because it would overwrite the previous y values (look carefully and you will know what i mean)
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  23. #23
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    That was an example... there's no reason to do it that way, you only need to think.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  24. #24
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type MyType
    4.     y(4) As Integer
    5. End Type
    6. Dim x(4) As MyType
    7.  
    8.  
    9. Private Sub Form_Load()
    10.     Dim i As Integer, j As Integer
    11.    
    12.     For i = 0 To 4
    13.         For j = 0 To 4
    14.             x(i).y(j) = i * 5 + j
    15.         Next j
    16.     Next i
    17.    
    18.     For i = 0 To 4
    19.         For j = 0 To 4
    20.             Debug.Print x(i).y(j)
    21.         Next j
    22.     Next i
    23.  
    24.  
    25. End Sub

    There you have.... 25 different numbers
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

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