?
Solved

Creating many objects of the same type with a for loop

Posted on 2006-03-29
4
Medium Priority
?
135 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
0
Comment
Question by:g-spot
  • 2
  • 2
4 Comments
 

Author Comment

by:g-spot
ID: 16325148
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16325152
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
 

Author Comment

by:g-spot
ID: 16325200
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
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 16325643
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question