Visual Basic .NET Help
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.w开发者_如何学JAVAork_city = list.Item(12)
contact.work_state = list.Item(13)
contact.work_zip = list.Item(14)
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.
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.
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.
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
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
Easy, use a ForEach loop to iterate through the members in contact. I am assuming "contact" is a class with all the members listed as being public.
Next use a counter variable (i) to go from 0 to (list.Items.Count - 1). Initialize this counter variable OUTSIDE the ForEach loop.
Then assign the first listitem (When i = 0) to the first member in contact. After assigning, increment i (i+=1) and then close the loop.
Now i will automatically increment each time the loop runs and will assign the required data to the contact member.
精彩评论