I have a generic List(Of Customer). The customer class has a name, address, and phone number properties. I also have a property of another class that accepts a customer name array. I am able to do this by doing the following:
Dim names As String()
Dim i As Integer = 0
'customer.GetCustomers is a List(of Customer)
For Each customer As Customer In customer.GetCustomers()
ReDim Preserve names(i)
names(i) = customer.Name
i += 1
Next
Then to set it:
'CustomerNames is a String()
Class.CustomerNames = names
Is there a better way to convert this to a string array? Any help is appreciated. Thanks.
To convert the contents of an ArrayList to a String, create a StringBuffer object append the contents of the ArrayList to it, finally convert the StringBuffer object to String using the toString() method.
To convert ArrayList to array in Java, we can use the toArray(T[] a) method of the ArrayList class. It will return an array containing all of the elements in this list in the proper order (from first to last element.)
You can use LINQ (Pardon my VB, I prefer C#)
Dim queryResults = From cust In customer.GetCustomers() Select cust.Name
Class.CustomerNames = queryResults.ToArray()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With