list of integer to comma delimited string
Asked Answered
C

1

7

I have tried this several ways and keep getting an error each time I try.

This is using .net 3.5 with asp.net(forms) and vb.net.

Examples:

Dim _registrations = New List(Of Integer)

Dim regList As String

Dim ListOfReg = _registrations.convertall(Of String)(Function(i As Integer) i.ToString())

regList = String.Join(",", ListOfReg.ToArray())

Error message:

Overload resolution failed because no Public 'convertall' can be called with these arguments: 'Public Function ConvertAll(Of String)(converter As System.Converter(Of Integer,String)) As System.Collections.Generic.List(Of String)': Argument matching parameter 'converter' cannot convert from 'VB$AnonymousDelegate_0(Of Integer,String)' to 'Converter(Of Integer,String)'.

Other attempt:

regList = String.Join(",", (_registrations.Select(Function(reg) reg.ToString()).ToArray()))

Error message:

Public member 'Select' on type 'List(Of Integer)' not found.

Any help is appreciated.

Thanks.

Courtesan answered 27/8, 2012 at 19:8 Comment(5)
try to add 'Imports System.Linq' at the top of your vb file, and see if you still have errors.Lagniappe
I still get both errors after adding that.Courtesan
I changed my answer, the new solution should work for 3.5.Yale
On second thought, There is nothing wrong with your "other attempt", it works fine for me. Sorry for the spam.Yale
It seems there was a scope issue with the variable due to it being a private variable in a class, once I moved the variable inside the scope of the function it worked. Thanks.Courtesan
Y
21

this should work , I guess it is the square brackets on the select?

.NET 3.5 solution

Dim integers As List(Of Integer) = New List(Of Integer)
integers.Add(1)
integers.Add(2)
integers.Add(3)

Dim commas As String = String.Join(",", integers.[Select](Function(i) i.ToString()).ToArray())

MessageBox.Show(commas)

Below is the.NET 4.0 Solution

Dim integers As List(Of Integer) = New List(Of Integer)
        integers.Add(1)
        integers.Add(2)
        integers.Add(3)


        Dim commas As String = String.Join(",", integers.ToArray)

        MessageBox.Show(commas)
Yale answered 27/8, 2012 at 19:47 Comment(5)
you still have to convert the ints in strings, String.Join only accepts arrays of string - MSDNLagniappe
Unable to cast object of type 'System.Int32[]' to type 'System.String[]'.Courtesan
I ran the above code with options strict and option explicit on, is it failing for you?Yale
@MSDN - Matthieu - Might want to check that bit of info. There is an overload that accepts an Object[] array that seems to do the job nicely, as I wrote almost the identical snipped to that PatFromCananda spun out (although in C#), and it works just fine...per MSDN, that particular overload just calls the ToString() method against each Object in the array...Englishism
@DavidW : that overload appeared only in net4.0+. It won't work for the OP.Lagniappe

© 2022 - 2024 — McMap. All rights reserved.