tags:

views:

80

answers:

2

What is the simplest/best practices way to get a comma separated list of the integers in the Integer array levels?

Dim levels(5) As Integer
Dim levelsStr As String

'put values in levels'

'Attempt 1: Failed'
levelsStr = String.Join(", ", levels) ' <- Error on levels'
'Value of type "1-dimensional array of Integer" cannot be converted'
'to "1-dimensional array of String" because "Integer" is not derived'
'from "String".'
+4  A: 

Try this

levelsStr = String.Join(", ", levels.Select(Function(x) x.ToString()).ToArray())
JaredPar
Array.ConvertAll is a possible alternative
Jimmy
@Jimmy very true, but LINQ is the ultimate hammer and enumerations are the nail.
JaredPar
+1  A: 

Here's how you could do it with Array.ConvertAll:

Dim converter = New Converter(Of Integer, String)(Function(num) num.ToString)
Dim y = String.Join(", ", Array.ConvertAll(x, converter))
Meta-Knight