Results 1 to 29 of 29

Thread: Open arrays

  1. #1

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371

    Question Open arrays

    Hi there,

    How do I define a two dimensional open array?

    does this work?

    dim test(,1 to 5) as integer

    thanks in advance

  2. #2
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    As far as i know i do not believe this is possible in VB classic.

  3. #3

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    so what should I use?

    array of array?

    example:

    dim A(1 to 5) as integer
    dim B() of A(1 to 5)

    is it possible??

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    That isn't possible in VB6. What you need is .NET, where all languages have this feature.

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    but imagine I don't have .NET ( )...

    is it Ok if I just open a 2 dimension array and then dimension it? I mean does the program becomes slower or uses too much resources?

    something like this:

    dim A(,) as integer

    ... program code...

    redim A(1 to 10, 1 to 3)

  6. #6

  7. #7

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    well that should do the trick... I wonder why I didn't though of that earlier ....

    .... forgive me for my ignorance but is it the same of an open array??

    .... 2nd dum question: you mentioned "redim preserved" is it really a function? and whats the difference to just Redim??

  8. #8
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    You can not Redim Preserve an allready dimensionlised Array.

  9. #9

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    I think I saw in some code somewhere this:

    dim A()() of integer

    was it my imagination?.. or is it possible ?

  10. #10
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    You can go with a single dimension!

    VB Code:
    1. Dim Something() As Integer
    2.  
    3. 'later on...
    4. '(we start from index 0, simplest to go with in the end...)
    5. ReDim Preserve Something(MainItems * SubItems - 1)
    6. 'then when handling it...
    7. For A = 0 To UBound(Something)
    8.     Debug.Print Something(A), "Mainitem: " & A \ SubItems, "Subitem: " & A Mod SubItems
    9. Next A

    Shouldn't be too hard and you can freely add more and remove stuff as you want.

  11. #11

  12. #12

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    Originally posted by MartinLiss
    You're right but you can do

    VB Code:
    1. Dim x() As Integer
    2.  
    3. ReDim x(4, 1 To 5)
    4. x(3, 1) = 5
    5. MsgBox x(3, 1)
    sou what you are saying is I can redim an open array to whatever dimensions I want...?? can I assume that?

    by the way... still I have this doubts:

    Whats the difference from an open array and a defined (and latter "Redim"ed) array??

    "redim preserved" is it really a function? and whats the difference to just Redim??

  13. #13
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    Yes redim preserved is reaaly a function yeas i know it does not show in inteli scence only one of a hand full of key words and functions that does not.

    the difference between redim preserved and just redim is:

    if you use redim preserved any data in the array will stay as long as it does not shrink using redim only will earase all data stored in the array.

    Hope this helps!!!

  14. #14

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    Ok... the first paragraph I didn't understand... (I'm guessing you were making fun at my english- since you're from england- but that's OK.. 'cause I know it sucks )

    as for the 2nd paragraph what you were saying is that if I use just Redim, VB will initialize the variable, hence losing all the data that was in the array... Am I correct?

  15. #15
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175
    Originally posted by segrobiur
    sou what you are saying is I can redim an open array to whatever dimensions I want...?? can I assume that?

    by the way... still I have this doubts:

    Whats the difference from an open array and a defined (and latter "Redim"ed) array??

    "redim preserved" is it really a function? and whats the difference to just Redim??
    Yes you can Redim to whatever dimensions you want.

    In addition to what Bomdrop said you should be aware that you can only Redim the second dimension, which in my eample means that you could do

    ReDim Preserve x(5, 1 To 6)

    but not

    ReDim Preserve x(6, 1 To 5)

  16. #16

  17. #17
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    Hi segrobiur not making fun of your English even thought I'm an born and bread Englishman I have a very poor grasp of the language myself my spelling is awfull.

    what i ment to say was redim preserved does not appear in the Intelliscence you know when you press ctrl+space and get the drop down of keywords.

    Sorry if you thought i was having a swip at you


  18. #18
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667
    Hi segrobiur not making fun of your English even thought I'm an born and bread Englishman I have a very poor grasp of the language myself my spelling is awfull.

    what i ment to say was redim preserved does not appear in the Intelliscence you know when you press ctrl+space and get the drop down of keywords.

    Sorry if you thought i was having a swip at you


  19. #19

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    last couple of doubts:

    Is there no way to dimension a 2 variable array?

    ex:

    dim A() as integer

    redim Preserve A(1 to 5, 1 to 6)
    ..(code)..
    redim Preserve A(1 to 20, 1 to 6)


    difference between using.

    dim A(0,1 to 10) as integer
    redim preserve A(5,1 to 10)

    and

    dim B() as integer
    redim preserve B(5,1 to 10)

    (processing time and resources needed?)

  20. #20
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    dim A(0,1 to 10) as integer
    redim preserve A(5,1 to 10)

    wouldn't work, because you can't redim a fixed size array.


    redim Preserve A(1 to 5, 1 to 6)
    ..(code)..
    redim Preserve A(1 to 20, 1 to 6)

    wouldn't work, because you can redim only the last items. Blame the creators of VB for not allowing this.


    And still, one dimension array would work fine for all your needs, even though you need to do a lot of stuff manually. Hmm... now that I think about it, it could be possible to create a class module which you could control somewhat the way you want - though the syntax wouldn't be as simple. Hmm, yup, it'd require some evil workarounds... but it'd be possible.



    Edit And if want to know, one dimension array starting from index 0 would be a LOT faster than any of the multidimension arrays or any array starting from index 1.

  21. #21

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    so from what I understand of all this posts it's better to create a large array when I don't know how many variables I will need.

    so what's the point of an open array in VB??

  22. #22

  23. #23

    Thread Starter
    Hyperactive Member segrobiur's Avatar
    Join Date
    May 2004
    Location
    Portugal
    Posts
    371
    ... off course you didn't ... it's just a little inside though of mine!!

    it's easier (and more flexible for me) to use a large array and then use a counter to determine the size of the array I want to use.... since I don't loose any significant processing time and/or memory...

    many thanks for the answers everybody

  24. #24
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,242
    If you will be trying to redim any dimension other than the last one on an array, you will have to decide how much effort you want to go through. You have three options:

    1) Make it big enough to handle whatever.

    The drawback to this is that you may not know how big whatever is. You could also say that you will be wasting space, but that is hardly a concern anymore.

    2) You could write a function that does the redimming for you. This would be the slowest, but most flexible, and perhaps the easiest technique. Worth considering if blazing speed is not necessary. The function would essentially copy the array to a temporary array, redim the array to the proper size, and then copy the data back from the temporary.

    3) Use Merri's suggestions. I doubt this is as fast as number 1 becasue of the multiplication. I would assume that VB does pointer arithmetic behind the scenes for multidimensional arrays, but I have never tested, and Merri just might have.

  25. #25
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    This will do what you need...

    use
    Code:
    Dim A(,)
    oops -> A()
    then redim it to whatever you want!

    you can only Redim the second dimension
    ...if you dim them to a value in the first place

    you can also dim(,) as insert-type oops, dim() as integer

    I do this with integers, and use ReDim to clear the array, making it the same size every time. I'm about to copy them to a smaller array, which I will DIM to the correct parameters.
    I hope to use
    NewArray(newa,newb) = OldArray(,)
    but haven't tried it yet.
    Last edited by dglienna; Sep 23rd, 2004 at 01:46 PM.

  26. #26
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    dglienna, like if that worked. Test the code before you suggest anything We've already talked a lot about what you can do and what you can't do.

  27. #27
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    I have the first part done. An open array in a module, and then every time I start a sub, I clear the old one by

    Code:
    ReDim Array(100,100) As Integer
    except that I use
    Code:
    Dim Array() As Integer
    sorry about the confusion. I thought thats how i resolved it.
    it works, though.

    haven't tried to copy it, yet. haven't looked at that part for awhile.
    Last edited by dglienna; Sep 23rd, 2004 at 01:47 PM.

  28. #28
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    dim A() as integer

    redim Preserve A(1 to 5, 1 to 6)
    ..(code)..
    redim Preserve A(1 to 20, 1 to 6)
    you can't redim it after using it. you have to do it first, or all the above rules apply.

    I just made (100,100) into (1,100) and got no errors. When I tried to PRESERVE, i got subscript out of range. there was noting in the array, but i don't think that matters.

  29. #29
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    This is how you can copy array stuff from one to another... but this works only with fixed item length arrays, basically with everything except strings and variants.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    4. Private Sub Form_Load()
    5.     Dim A() As Byte
    6.     Dim B() As Byte
    7.     Dim C As Byte
    8.     ReDim A(3, 4)
    9.     A(3, 4) = 1
    10.     B = A
    11.     ReDim A(4, 5)
    12.     For C = 0 To UBound(B)
    13.         CopyMemory A(0, C), B(0, C), 4
    14.     Next C
    15.     MsgBox A(3, 4)
    16. End Sub

    As you can see, it seems that the LAST index is the primary item. When I increase later item by one, I jump by the number of the stuff before it (I checked with VarPtr).

    Another thing: Be careful with CopyMemory! If you copy from wrong place or copy too much, you can really get into problems.


    And if you wonder what was the point in this example: I showed how to change the array size as you want to by using a temporary array.

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