tags:

views:

80

answers:

6

How can I get this code into a loop?

contact.first_name = list.Item(0)
contact.middle_name = list.Item(1)
contact.last_name = list.Item(2)
contact.age = list.Item(3)
contact.mobile_phone = list.Item(4)
contact.home_phone = list.Item(5)
contact.work_phone = list.Item(6)
contact.home_street = list.Item(7)
contact.home_city = list.Item(8)
contact.home_state = list.Item(9)
contact.home_zip = list.Item(10)
contact.work_street = list.Item(11)
contact.work_city = list.Item(12)
contact.work_state = list.Item(13)
contact.work_zip = list.Item(14)
+3  A: 

You can't. Even if you used something like reflection to enumerate the fields in contact and assign them that way, since there's no inherent ordering of those members, you'd still not be able to do the loop.

Billy ONeal
A: 

I assume you want to loop list.Item. You can do it but you'll end up with more code than you have already.

For example

For i As Integer = 0 To list.Item.Count - 1
  Select Case i
    Case 1
      contact.middle_name = list.Item(i)
    Case 2
      contact.last_name = list.Item(i)
    Case 3
      contact.age = list.Item(i)
    ...
  End Select
Next

As you can see it probably is not worth doing.

Jonathan
+1  A: 

First, you would need some way to associate index with the name of a property. Probably the best way to do this is to create a list of pairs that store the property name and an index:

Class Info
  Public Property Name As String
  Public Property Index As Integer
End Class

Then, you'd need to create a list with elements that represent the associations:

Dim associations = { 
   New Info With { .Name = "first_name", .Index = 0 }, _
   New Info With { .Name = "second_name", .Index = 1},  ... }

Now, you could use a simple For loop and Reflection to set the properties:

Dim contType = contact.GetType()
Dim empty As Object(0)
For Each assoc In associations
  contType.GetProperty(assoc.Name) _
          .SetValue(contact, list.Item(assoc.Index), empty)
Next

I'm not a VB expert and I didn't try the code, but something along these lines should work. Anyway, it really depends on the scenario - in some cases, there is no better way than what you're using currently.

Tomas Petricek
A: 

I need it in a loop because the list can go to infinity

    contact.first_name = list.Item(0)
    contact.middle_name = list.Item(1)
    contact.last_name = list.Item(2)
    contact.age = list.Item(3)
    contact.mobile_phone = list.Item(4)
    contact.home_phone = list.Item(5)
    contact.work_phone = list.Item(6)
    contact.home_street = list.Item(7)
    contact.home_city = list.Item(8)
    contact.home_state = list.Item(9)
    contact.home_zip = list.Item(10)
    contact.work_street = list.Item(11)
    contact.work_city = list.Item(12)
    contact.work_state = list.Item(13)
    contact.work_zip = list.Item(14)

    contact.first_name = list.Item(15)
    contact.middle_name = list.Item(16)
    contact.last_name = list.Item(17)
    contact.age = list.Item(18)
    contact.mobile_phone = list.Item(19)
    contact.home_phone = list.Item(20)
    contact.work_phone = list.Item(21)
    contact.home_street = list.Item(22)
    contact.home_city = list.Item(23)
    contact.home_state = list.Item(24)
    contact.home_zip = list.Item(25)
    contact.work_street = list.Item(26)
    contact.work_city = list.Item(27)
    contact.work_state = list.Item(28)
    contact.work_zip = list.Item(29)

    contact.first_name = list.Item(30)
    contact.middle_name = list.Item(31)
    contact.last_name = list.Item(32)
    contact.age = list.Item(33)
    contact.mobile_phone = list.Item(34)
    contact.home_phone = list.Item(35)
    contact.work_phone = list.Item(36)
    contact.home_street = list.Item(37)
    contact.home_city = list.Item(38)
    contact.home_state = list.Item(39)
    contact.home_zip = list.Item(40)
    contact.work_street = list.Item(41)
    contact.work_city = list.Item(42)
    contact.work_state = list.Item(43)
    contact.work_zip = list.Item(44)
Daniel
A: 

If you need it to go to infinity, you probably want a function that takes a contact, a list, and a starting index.

Public Sub FillContact(ByVal contact As Contact, values As IList(Of Object),
                       startingIndex As Integer)
    'error checking here, such as ensuring you have a contact and value
    'list and that startingIndex + number of properties < values.Count

    contact.first_name = values(startingIndex)
    contact.middle_name = values(startingIndex + 1)
    '...
    contact.work_state = value(startingIndex + 13)
    contact.work_zip = value(startingIndex + 14)
End Sub

Then you can call this function in a loop, something like

Dim contacts As New List(Of Contact)
'load the values
Dim values() As Object = GetValues()
For i = 0 To values.Length Step NumberOfProperties
    Dim nextContact As New Contact()
    FillContact(nextContact, values, i)
    contacts.Add(nextContact)
Next
Gideon Engelberth
A: 

I managed to get it working in a loop statement.

    For x As Integer = 1 To list.Count / 15
        Dim contact As New Contact
        my_contacts.Add(contact)
    Next

    Dim i As Integer = 0

    For Each contact In my_contacts
        With contact
            .first_name = list.Item(i)
            i += 1
            .middle_name = list.Item(i)
            i += 1
            .last_name = list.Item(i)
            i += 1
            .age = list.Item(i)
            i += 1
            .mobile_phone = list.Item(i)
            i += 1
            .home_phone = list.Item(i)
            i += 1
            .work_phone = list.Item(i)
            i += 1
            .home_street = list.Item(i)
            i += 1
            .home_city = list.Item(i)
            i += 1
            .home_state = list.Item(i)
            i += 1
            .home_zip = list.Item(i)
            i += 1
            .work_street = list.Item(i)
            i += 1
            .work_city = list.Item(i)
            i += 1
            .work_state = list.Item(i)
            i += 1
            .work_zip = list.Item(i)
            i += 1
        End With
    Next
Daniel