Results 1 to 21 of 21

Thread: Timer1 control array to Redim preserve

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    70

    Timer1 control array to Redim preserve

    I have a timer control which I want to create an array of. Then at some point in my program I will need to Redim preserve that control array.

    How would I do this?

    Right now I have placed a timer control on my form then made a copy of it to create an array. When I try this line of code:

    Code:
    Redim preserve Timer1(2 to 10)
    it's telling me "variable not defined", referring to the timer control.
    I believe I need to create a new variable and set it equal to the Timer control array, but not sure how to code this.

    ?????
    "never trouble trouble til trouble troubles you"

  2. #2
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    to make arrays, use the Load() function, ie

    dim IndexToLoad as integer

    IndexToLoad = Timer1.UBound + 1
    Load Timer1(IndexToLoad)
    Timer1(IndexToLoad).interval=500
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    70
    If I needed to Redim Preserve this Timer control, how can I do this? It's asking for a defined variable instead of Timer1.
    "never trouble trouble til trouble troubles you"

  4. #4

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    70
    Let's say I'm monitoring students while they take a test. Each student has a fixed amount of time to take the test. The catch is, students can arrive at different times. So each time a student arrives I will load a new timer array element to keep track of their test time. Then as a student finishes, I unload that timer and Redim Preserve the array.

    I know this sounds a little weird but I'm determined to get this to work using a timer control array.
    "never trouble trouble til trouble troubles you"

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Add a timer to your form and set it's Index property to one. Then do

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Load Timer1(Timer1.Count)
    4.    
    5. End Sub

    There is no way to Redim a control array. You should simply Unload those you don't need. In other words when the 2nd student finishes you would do

    Unload Timer1(1)

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    70
    That's what I was afraid you would say. I'm able to load the timers and unload them. I was just uncomfortable with having my array "chopped" up.......elements being (0, 2, 3, 5, 9, etc..) as I continued to unload and load more timers.

    By using the timer count property, will it keep incrementing when a new timer is loaded, no matter how many are unloaded during that period. In other words, let's say my timer count is at 7 and I unloaded 3 timers, is my timer count still 7? Or does it decrement as timers are unloaded?
    "never trouble trouble til trouble troubles you"

  8. #8
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    as long as you code right, that shouldn't be a problem unless you plan to have more than 32767 students per day. you COULD perhaps place it into a collection:

    VB Code:
    1. Dim col As New Collection
    2. Private Sub Form_Load()
    3.     Load Timer1(Timer1.UBound + 1) 'load a new timer    
    4.     col.Add Timer1(Timer1.UBound) 'add that new timer to the collection
    5.    
    6.     col.Item(1).Interval=1000 'to reference it
    7.     'and then when you want to unload the timer:
    8.     unload col.item(1)
    9.     col.remove 1
    10. End Sub

    you still need to UNLOAD the timer before removing it from the collection
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by Jason Carden
    That's what I was afraid you would say. I'm able to load the timers and unload them. I was just uncomfortable with having my array "chopped" up.......elements being (0, 2, 3, 5, 9, etc..) as I continued to unload and load more timers.

    By using the timer count property, will it keep incrementing when a new timer is loaded, no matter how many are unloaded during that period. In other words, let's say my timer count is at 7 and I unloaded 3 timers, is my timer count still 7? Or does it decrement as timers are unloaded?
    The Count property will reflect the total number of timers that exist at any. For example if you have timer1 objects with indexes of 0, 2, 3, 5 and 9 the Count would be 5.

    If you want to delete all the added timers (you can't delete the zeroeth timer1) then you can do

    VB Code:
    1. Dim ctl As Control
    2.     For Each ctl In Controls
    3.         If TypeOf ctl Is Timer Then
    4.             If ctl.Index <> 0 Then
    5.                 Unload ctl
    6.             End If
    7.         End If
    8.     Next

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    70
    Buggyprogrammer, thanks for the example of the collection. I believe this still doesn't solve my problem of "renumbering" all the elements or items into a consecutive line of numbers...1, 2, 3, 4, 5...etc, without losing my data.

    I did read that it wasn't necessary to Redim a collection, but I don't understand why that is?? What's the alternative?


    Martin, if my controls were different, not just a Timer, that would definitely come in handy....pretty slick stuff!!
    "never trouble trouble til trouble troubles you"

  11. #11

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    70
    Hmmm...I ran the example Buggy.. supplied. I loaded 5 timers, index's were 1 - 5, but when I removed index 2, what was left running were 1, 3, 4 & 5!!!???

    Here's what I wrote:

    Code:
    Private col As New Collection
    Private x As Integer
    
    Private Sub Command1_Click() 'unload timer
    Unload col.Item(2)
    col.Remove 2
    End Sub
    
    Private Sub Command2_Click() 'load timer
    Dim a As Integer
    a = Timer1.UBound + 1
    Load Timer1(a) 'load a new timer
    Timer1(a).Enabled = True
    col.Add Timer1(a) 'add that new timer to the collection
    col.Item(a).Interval = 5000  'to reference it
    End Sub
    
    Private Sub Timer1_Timer(Index As Integer)
    x = x + 1
    Text1.Text = x
    End Sub
    "never trouble trouble til trouble troubles you"

  13. #13
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    VB Code:
    1. Private Function FindFree_Timer As Integer
    2. Dim intIterate As Integer
    3.  
    4.    FindFree_Timer = 0
    5. On Error Goto ErrorHandler
    6.    For intIterate = 1 To Timer1.Ubound
    7.       Timer1(intIterate).Tag = Timer1(intIterate).Tag  
    8.    Next
    9. On Error Goto 0
    10.  
    11.    'FindFree_Timer = 0  since all are loaded
    12.    Exit Function
    13.  
    14. ErrorHandler:
    15. 'Goes here if instance does not exist
    16.    FindFree_Timer = intIterate
    17.    Err.Clear
    18. End Sub
    19.  
    20.  
    21. Public Function Assigned_TimerIndex As Integer
    22. Dim intRetIndex As Integer
    23.  
    24.    intRetIndex = FindFree_Timer
    25.    If intRetIndex = 0 Then
    26.       Load Timer1(Timer1.Ubound + 1)
    27.       Timer1(Timer1.Ubound).Interval = 5000   'Ubound updates
    28.       Assigned_TierIndex = Timer1.Ubound
    29.    Else
    30.       Load Timer1(intRetIndex)
    31.       Timer1(intRetIndex).Interval = 5000
    32.       Assigned_TierIndex = intRetIndex
    33.    End if
    34. End Sub

    That should fill up your holes. Admittedly, they won't be in order of creation.... but do you really want to bother with that? In the end, you wont have miore than 32K students being assigned timers anyway.

    You can modify the concept to skip over the holes if your iterating through the control array. Either create a list of loaded indices or use On Error Resume Next to ignore the error with unloaded indices.
    Last edited by leinad31; Jan 15th, 2004 at 02:08 PM.

  14. #14
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    I can't figure out why you need more than one timer. All you need to do is have one timer that updates each second, then check an array of starttimes to see if the desired time has expired or not. Wouldn't that be a hell of a lot easier and faster???

  15. #15
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Originally posted by ae_jester
    I can't figure out why you need more than one timer. All you need to do is have one timer that updates each second, then check an array of starttimes to see if the desired time has expired or not. Wouldn't that be a hell of a lot easier and faster???
    So true

  16. #16
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by Jason Carden
    Hmmm...I ran the example Buggy.. supplied. I loaded 5 timers, index's were 1 - 5, but when I removed index 2, what was left running were 1, 3, 4 & 5!!!???
    Why the confusion? You removed index 2 so now it's gone.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    70
    No confusion, I guess. When you said it removes the "hole" I thought it was was going to renumber the elements/items into consecutive order.

    Oh well, ...preciate all your help.
    "never trouble trouble til trouble troubles you"

  18. #18

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    70
    OK, again confused! Earlier when I ran the collection test app and removed item (2), the items that remained were 1, 3, 4 and 5. I was expecting it to be 1, 2, 3, 4!!
    "never trouble trouble til trouble troubles you"

  20. #20
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I'm a fan of collections but I'm not sure what advantage there is in this case. I modified buggy's code as shown.

    VB Code:
    1. Private Sub Form_Load()
    2.     Load Timer1(Timer1.UBound + 1) 'load a new timer
    3.     col.Add Timer1(Timer1.UBound) 'add that new timer to the collection
    4.    
    5.     col.Item(1).Interval = 1000 'to reference it
    6.    
    7.    
    8.     Load Timer1(Timer1.UBound + 1) 'load a new timer
    9.     col.Add Timer1(Timer1.UBound) 'add that new timer to the collection
    10.    
    11.     col.Item(2).Interval = 1000 'to reference it
    12.    
    13.    
    14.     Load Timer1(Timer1.UBound + 1) 'load a new timer
    15.     col.Add Timer1(Timer1.UBound) 'add that new timer to the collection
    16.    
    17.     col.Item(3).Interval = 1000 'to reference it
    18.     'and then when you want to unload the timer:
    19.     Unload col.Item(2)
    20.     col.Remove 2
    21. End Sub
    If you put a breakpoint at the Unload line you will find that at that point you have three members of the collection and if you type the following in the immediate window

    ?col(1).Index
    ?col(2).Index
    ?col(3).Index

    you'll get 1, 2, 3 which are the indexes of the control array and not the item number of the control array. If you let the code go to End Sub and you do the same thing in the immediate window you will find that you have only ?col(1).Index will say "1", while col(2).Index will say 3. In other words the collection hole has been filled but the control array (2) has been deleted and there still is a hole in that array.

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    70
    Clear as mud!!!...no, just kidding. I don't think we can get better proof than that....eh.

    Thanks for deciphering this for me!!
    "never trouble trouble til trouble troubles you"

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