0

I have the code below that requires me to convert a character array to string array, but I get the following error: Option Strict On disallows implicit conversions from '1-dimensional array of Char' to 'System.Collections.Generic.IEnumerable(Of String)'

    Dim lst As New List(Of String)
    lst.AddRange(IO.Path.GetInvalidPathChars())
    lst.AddRange(IO.Path.GetInvalidFileNameChars())

    lst.Add("&")
    lst.Add("-")
    lst.Add(" ")

    Dim sbNewName As New StringBuilder(orignalName)
    For i As Integer = 0 To lst.Count - 1
        sbNewName.Replace(lst(i), "_")
    Next

    Return sbNewName.ToString

I tried using a converter through Array.ConvertAll, but couldn't find a good example, I could use a loop, but thought there would be a better way. Could anyone help?

2 Answers 2

2

Just change the lst.AddRange lines to this:

Array.ForEach(Path.GetInvalidPathChars(), AddressOf lst.Add)
Array.ForEach(Path.GetInvalidFileNameChars(), AddressOf lst.Add)
Sign up to request clarification or add additional context in comments.

Comments

1

VB Linq syntax is not a strong point for me, but to get you started, consider selecting the items from the character arrays and converting each to string. In C#, that would be

lst.AddRange(System.IO.Path.GetInvalidPathChars().Select(c => c.ToString()); 

Thanks to NYSystemsAnalyst for the VB syntax

lst.AddRange(System.IO.Path.GetInvalidPathChars().Select(Function(c) c.ToString()))

Without Linq, you could simply iterate in a loop explicitly

For Each c as Char in System.IO.Path.GetInvalidPathChars()
    lst.Add(c.ToString())
Next c

2 Comments

This is also an option. Here is the VB syntax: lst.AddRange(Path.GetInvalidPathChars().Select(Function(c) c.ToString())) lst.AddRange(Path.GetInvalidFileNameChars().Select(Function(c) c.ToString()))
@MrShoubs, added non-Linq answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.