We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Creating many objects of the same type with a for loop

g-spot
g-spot asked
on
Medium Priority
155 Views
Last Modified: 2010-04-23
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
Comment
Watch Question

Author

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
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
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.

Author

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?
Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.