Results 1 to 4 of 4

Thread: [RESOLVED] Loop to add list box items

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Resolved [RESOLVED] Loop to add list box items

    Hello there,

    I have attmpted a For To Next Loop and a Do While Not Loop to add all the items from my list box.
    Here's a look at both loops.

    1)
    VB Code:
    1. Private Sub List0_DblClick(Cancel As Integer)
    2. Dim s As Integer
    3. For s = 0 To List0.ListCount
    4. List1.AddItem List0.ItemData(s)
    5. Next s
    6. End Sub

    This loop adds item as desired but prompts an error message at the end.
    Run-time error 94 Invalid use of Null.

    2)
    VB Code:
    1. Private Sub List0_DblClick(Cancel As Integer)
    2. Dim i As Integer
    3. i = List0.ListCount
    4. Do While Not i = 0
    5. i = i - 1
    6. List1.AddItem List0.ItemData(i)
    7. Looo   
    8. End Sub

    This loop adds item from bottom to top since it starts from ListCount's value and - 1.

    I'll like to have my items added the correct way without the error, can anyone comment on my loops and give suggestions?
    will be more than glad to learn new ways of looping.

    Thank You


    Astro
    Last edited by Astro; Aug 30th, 2005 at 03:20 AM.

  2. #2
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: Loop to add list box items

    VB Code:
    1. Private Sub List0_DblClick(Cancel As Integer)
    2. Dim s As Integer
    3. For s = 0 To List0.ListCount-1
    4. List1.AddItem List0.ItemData(s)
    5. Next s
    6. End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: Loop to add list box items

    VB Code:
    1. Dim i As Integer
    2. Dim x As Integer
    3. i = List0.ListCount
    4. x = 0
    5. Do While Not x = i
    6. List1.AddItem List0.ItemData(x)
    7. x = x + 1
    8. Loop

    I got it too! Haha just saw your reply moinkhan.
    Your's definitely shorter. Any pros/cons besides length of code?
    Comments appreciated =P

    Astro

  4. #4
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: Loop to add list box items

    Quote Originally Posted by Astro
    VB Code:
    1. Dim i As Integer
    2. Dim x As Integer
    3. i = List0.ListCount
    4. x = 0
    5. Do While Not x = i
    6. List1.AddItem List0.ItemData(x)
    7. x = x + 1
    8. Loop

    I got it too! Haha just saw your reply moinkhan.
    Your's definitely shorter. Any pros/cons besides length of code?
    Comments appreciated =P

    Astro
    I dont know about any performance difference.. but as u can see.. the lower and upper bounds of loop are set at the start of loop.. it should be faster...
    This i know for sure.. Do While has to check the condition everytime...

    and... my coding style suggests that this job is for For..Next... Do While is for other complex conditions... for counter based loops.. use For..Next

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