Results 1 to 6 of 6

Thread: [RESOLVED] ReDim Array (different question!)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    [RESOLVED] ReDim Array (different question!)

    Hi, again.
    Same program as described here.
    The Queue system for this program is based off of arrays, and it uses a FIFO system, so data is output in the order it is queued. BUT I want the option for someone to force data to be send AHEAD of everything else. I tried ReDim Preserve'ing the LBound - 1, but that gave me errors. Any thoughts on how I could add the force, it's stumping me.
    Last edited by R.a.B.B.i.T; Oct 2nd, 2004 at 05:04 PM.

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    1st option : Collections

    2nd option : Always have the Array from 0 to whatever. Then ReDim Preserve to the UBound(ArName)+1 and then :

    VB Code:
    1. for i=UBound(ArName)-1 to LBound(ArName)+1 step -1
    2. ArName(i+1)=ArName(i)
    3. next
    4. ArName(0)="newest value"

    I haven't tested it but I think you'll get the idea


    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    OK, tested working example :

    VB Code:
    1. Option Explicit
    2.  
    3. Dim Ar() As String
    4.  
    5. Private Sub Form_Load()
    6.     ReDim Ar(2)
    7.    
    8.     Ar(0) = "1st"
    9.     Ar(1) = "2nd"
    10.     Ar(2) = "3rd"
    11.    
    12.     ShowArray Ar
    13.    
    14.     AddFirst Ar, "manavo rocks!"
    15.    
    16.     ShowArray Ar
    17. End Sub
    18.  
    19. Private Sub ShowArray(Ar() As String)
    20.     Dim i As Integer
    21.    
    22.     For i = LBound(Ar) To UBound(Ar)
    23.         MsgBox Ar(i)
    24.     Next
    25. End Sub
    26.  
    27. Private Sub AddFirst(Ar() As String, NewValue As String)
    28.     Dim i As Integer
    29.    
    30.     ReDim Preserve Ar(UBound(Ar) + 1)
    31.    
    32.     For i = UBound(Ar) - 1 To LBound(Ar) Step -1
    33.         Ar(i + 1) = Ar(i)
    34.     Next
    35.    
    36.     Ar(LBound(Ar)) = NewValue
    37. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    And with collections, in case you are thining of changing from arrays (They are slower than arrays) :

    VB Code:
    1. Option Explicit
    2.  
    3. Dim Col As New Collection
    4.  
    5. Private Sub Form_Load()
    6.     Col.Add "1st"
    7.     Col.Add "2nd"
    8.     Col.Add "3rd"
    9.    
    10.     ShowCollection Col
    11.    
    12.     AddFirst Col, "manavo rocks!"
    13.    
    14.     ShowCollection Col
    15. End Sub
    16.  
    17. Private Sub ShowCollection(Col As Collection)
    18.     Dim i As Integer
    19.    
    20.     For i = 1 To Col.Count
    21.         MsgBox Col(i)
    22.     Next
    23. End Sub
    24.  
    25. Private Sub AddFirst(Col As Collection, NewValue As String)
    26.     Col.Add NewValue, , 1
    27. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255
    Thanks, I got it working the way I want based off of your example.

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Have fun


    Has someone helped you? Then you can Rate their helpful post.

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