Collection With Loop

  • 13 years ago

    Dim TeamAScoreCollection As New Collection

    Dim TeamBScoreCollection As New Collection

    Dim objtextbox As New Textbox

    'set scores in all Team A textboxes to 0

    For each objtextbox in TeamAScoreCollection


    Next objtextbox

    'set scores in all Team B textboxes to 0

    For each objtextbox in TeamBScoreCollection


    Next objtextbox

    By this code I have 2 collection variables that control a bunch of textboxes. Now rather than write 2 separate For Each loops, to write 0 in all the textboxes,  how can I do it in ONE loop?

    I have tried using the operators like And but apparently you cant do that. Any suggestions?

  • 13 years ago
    Why do you need a single loop?  The only way to do it using a for each loop would be to create a 3rd collection which combines the 2 you already have.  Then iterate through that collection.  If your collections are the same size you could use a regular for loop.
            For i As Integer = 0 To TeamAScoreCollection.Count - 1
                DirectCast(TeamAScoreCollection.Item(i), TextBox).Text = "0"
                DirectCast(TeamBScoreCollection.Item(i), TextBox).Text = "0"
            Next i
    Also, if your using VS2005 you should use a list instead of a general collection. You can create a list of textboxes like this.
    Dim TeamAScoreCollection As List(Of TextBox)
  • 13 years ago

    I am currently using VS2003. My Collections each have 14 textboxes.

    The only reason why I had a For Each loop was because that was the first thing that popped into my head to make it work....

    How can I make it work with the For Loop?

  • 13 years ago
    Check my last post again, the code is right there.

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There's no test like production” - Anon