![](/img/trans.png)
[英]Unable to cast the type 'System.String' to type 'System.Object'
[英]System.Object[] can't be converted to System.String[]
我正在嘗試轉換 C# 行
string[] phrases = text.Split (new[] { '\"' }, StringSplitOptions.RemoveEmptyEntries);
到 VB.NET。
我的嘗試是
Dim phrases() As String = text.Split (New Object() { """"c }, StringSplitOptions.RemoveEmptyEntries)
但是,我收到錯誤“System.Object[] 無法轉換為 System.String[]”。
請問我做錯了什么?
你可以使用任何在線 C# 到 VB 轉換器,你會得到這個:
Dim phrases As String() = text.Split(New String() {""""C}, StringSplitOptions.RemoveEmptyEntries)
所以錯誤的部分是使用 Object()。
正如 Plutonix 在他的評論中提到的,第一個參數是一個 char 數組(new[] { '\\"' } 如果不是一個 char 數組,則什么都不是),所以使用 char 數組也許有意義?
Dim phrases() As String = text.Split(New Char() {""""c}, StringSplitOptions.RemoveEmptyEntries)
現在在 VB 中,您甚至不需要說 'New Char()',因為 VB 知道 {""""c} 是一個字符數組:
Dim phrases() As String = text.Split({""""c}, StringSplitOptions.RemoveEmptyEntries)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.