Convert Array of strings to List(of T)

R

Rory Becker

I have an "array of strings".

I would like to convert this to a "List(Of Reference)"

A reference is an object which is creatable by passing one of said strings
to it's only constructor.


I can do this with a loop thus....
-------------------------------------------------------------
Dim Strings As String() = new string(){"String1", "String2"}
Dim References As New List(Of Reference)
For Each item In Strings
References.Add(New Reference(item))
Next
 
A

Armin Zingler

Rory said:
I have an "array of strings".

I would like to convert this to a "List(Of Reference)"

A reference is an object which is creatable by passing one of said
strings to it's only constructor.


I can do this with a loop thus....
-------------------------------------------------------------
Dim Strings As String() = new string(){"String1",
"String2"} Dim References As New List(Of Reference)
For Each item In Strings
References.Add(New Reference(item))
Next
-------------------------------------------------------------
...but I'd prefer not to.

Can I do this using any of the extension methods on IEnumerable etc
so that I don't have to implement this as a loop?

Yes, you can:

References = Array.ConvertAll(Of String, Reference) _
(Strings, Function(s As String) New Reference(s)).ToList

Though, ConvertAll is not an extension method; just a method of the Array
class. But in turn, ToList is an extension method.

Or, if you don't want to create a new List:

References.AddRange( _
Array.ConvertAll(Of String, Reference) _
(Strings, Function(s As String) New Reference(s)) _
)

(without extension method)


Armin
 
G

Göran Andersson

Rory said:
I have an "array of strings".

I would like to convert this to a "List(Of Reference)"
A reference is an object which is creatable by passing one of said
strings to it's only constructor.


I can do this with a loop thus....
-------------------------------------------------------------
Dim Strings As String() = new string(){"String1", "String2"}
Dim References As New List(Of Reference)
For Each item In Strings
References.Add(New Reference(item))
Next
-------------------------------------------------------------
...but I'd prefer not to.

Can I do this using any of the extension methods on IEnumerable etc so
that I don't have to implement this as a loop?

You will always have to loop through the values, either if you do it
yourself, or use a method that loops through the values.

You can for example do:

Dim References As List(Of Reference) = Strings.Select(Function(s As
String) New Reference(s)).ToList()

or:

Dim References As List(Of Reference) = (From s In Strings Select New
Reference(s)).ToList()
 
R

Rory Becker

Hello Göran,
You will always have to loop through the values, either if you do it
yourself, or use a method that loops through the values.

Absolutely agreed, but I was looking for a syntax more declarative or simple
than a straight loop.
You can for example do:

Dim References As List(Of Reference) = Strings.Select(Function(s As
String) New Reference(s)).ToList()

or:

Dim References As List(Of Reference) = (From s In Strings Select New
Reference(s)).ToList()

Both your examples are pretty much exactly what I was after.... thanks very
much
 
R

Rory Becker

Hello Armin,
References = Array.ConvertAll(Of String, Reference) _
(Strings, Function(s As String) New Reference(s)).ToList
Though, ConvertAll is not an extension method; just a method of the
Array class. But in turn, ToList is an extension method.

Or, if you don't want to create a new List:

References.AddRange( _
Array.ConvertAll(Of String, Reference) _
(Strings, Function(s As String) New Reference(s)) _
)

On first looks, your solution is very appealing.

My primary goal in this case is simple clarity of code. For this reason I
have gone with Goran's suggestion

Thanks very much for your answer though... I did not know about convertall
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top