2011-11-28 70 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的答案。 –

相關問題