2011-11-28 75 views
0

我有下面的代码需要我转换的字符数组字符串数组,但我得到了以下错误: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 

我试图用通过转换器Array.ConvertAll,但找不到一个好例子,我可以使用循环,但认为会有更好的方法。谁能帮忙?

回答

2

的lst.AddRange线就改成这样:

Array.ForEach(Path.GetInvalidPathChars(), AddressOf lst.Add) 
Array.ForEach(Path.GetInvalidFileNameChars(), AddressOf lst.Add) 
1

VB LINQ的语法是不是我的强项,但让你开始,可考虑从字符数组中选择的项目,每个转换成串。在C#中,这将是

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

感谢NYSystemsAnalyst的VB语法

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

没有LINQ的,你可以简单地在一个循环迭代明确

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

这也是一种选择。这里是VB语法:lst.AddRange(Path.GetInvalidPathChars()。Select(Function(c)c.ToString())) lst.AddRange(Path.GetInvalidFileNameChars()。Select(Function(c)c.ToString ))) – NYSystemsAnalyst

+0

谢谢,对不起,我没有提到代码库.Net 2.0 –

+0

@MrShoubs,添加了非Linq的答案。 –

相关问题