Creating many objects of the same type with a for loop

How do I use a For loop to declare and create a number of similar objects

In the example below I want to create 10 Person objects each with a FirstName and LastName taken from a textbox control

            For i = 2 To 10

                Dim Person As Person = New Person()

                Dim txtLocal As TextBox
                txtLocal = CType(plhOtherInsured.FindControl("txtFirstName" & LTrim(Str(i))), TextBox)
                If Not txtLocal Is Nothing Then
                    Person.FirstName = txtLocal.Text
                End If
                txtLocal = CType(plhOtherInsured.FindControl("txtLastName" & LTrim(Str(i))), TextBox)
                If Not txtLocal Is Nothing Then
                    Person.LastName= txtLocal.Text
                End If

            Next
g-spotAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

g-spotAuthor Commented:
I guess something like this is more correct. But I'm not sure to specify the variable i as part of the Person object name (so that each object has a unique name e.g. Person1, Person2, Person3 etc etc)

            Dim PolicyMembers As PersonCollection = New PersonCollection()
            Dim Person As Person

            For i = 2 To 10

                Dim txtLocal As TextBox

                Dim Person & i As Person = New Person
                txtLocal = CType(plhOtherInsured.FindControl("txtFirstName" & LTrim(Str(i))), TextBox)
                If Not txtLocal Is Nothing Then
                    Person & i.FirstName = txtLocal.Text
                End If

                txtLocal = CType(plhOtherInsured.FindControl("txtLastName" & LTrim(Str(i))), TextBox)
                If Not txtLocal Is Nothing Then
                    Person & i.LastName = txtLocal.Text
                End If

                PolicyMembers.Add(Person(i))

            Next
0
Carl TawnSystems and Integration DeveloperCommented:
Just like that, if you want to keep them I would suggest adding them to an ArrayList, or similar:

    Dim people As New ArrayList()

    For i = 2 To 10

          Dim oPerson As Person = New Person()

          Dim txtLocal As TextBox
          txtLocal = CType(plhOtherInsured.FindControl("txtFirstName" & LTrim(Str(i))), TextBox)
          If Not txtLocal Is Nothing Then
              oPerson.FirstName = txtLocal.Text
          End If
          txtLocal = CType(plhOtherInsured.FindControl("txtLastName" & LTrim(Str(i))), TextBox)
          If Not txtLocal Is Nothing Then
              oPerson.LastName= txtLocal.Text
          End If

          '// Add the new person to our collection
          people.Add(oPerson)

     Next


Hope this helps.
0
g-spotAuthor Commented:
Oh I see. I dont need to specifically name the Person objects. I can just create them and add them to the People collection.

Once they're in the People collection I'll be able to get at them by index?
0
Carl TawnSystems and Integration DeveloperCommented:
Yes. You can either access each one by index number:

    Dim Bob As Person = CType(people(0), Person)

Or, you can loop through all of them using a For Each loop:

   For Each p As Person In people
       MessageBox(p.FirstName)
   Next

Or, you can loop though all via index:

  For i As Integer = 0 To people.Count - 1
     MessageBox(CType(people(i), Person).FirstName)
  Next

Notice that when accessing by index you have to cast as the correct type "CType(people(0), Person". This is because the arraylist returns everything as Object.

If you use .Net 2.0 then you can use generics to create a "strongly-typed" list instead, or in 1.1 you can create you own strongly-typed collection object.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.