Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting a generic list to string array

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.

like image 240
Aaron Sanders Avatar asked Mar 08 '11 21:03

Aaron Sanders


People also ask

How do you return an ArrayList as a String?

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.

Can you convert an ArrayList to an array?

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.)


1 Answers

You can use LINQ (Pardon my VB, I prefer C#)

Dim queryResults = From cust In customer.GetCustomers() Select cust.Name
Class.CustomerNames = queryResults.ToArray()
like image 165
Dark Falcon Avatar answered Sep 28 '22 04:09

Dark Falcon