【问题标题】:System.Object[] can't be converted to System.String[]System.Object[] 无法转换为 System.String[]
【发布时间】:2016-10-02 21:47:45
【问题描述】:

我正在尝试转换 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[]”。

请问我做错了什么?

【问题讨论】:

  • 我在 VB 中不是很流利,但似乎 New Object() 部分是问题所在,因为它将新数组中的值实例化为 Objects 而不是 String s.
  • @Abion47:不,OP 正在使用采用 char 数组的重载 - 这就是他们在 C# 版本中传递 char 数组(而不是字符串数组)的原因。

标签: c# vb.net migration


【解决方案1】:

你可以使用任何在线的 C# 到 VB 转换器,你会得到这个:

Dim phrases As String() = text.Split(New String() {""""C}, StringSplitOptions.RemoveEmptyEntries)

所以错误的部分是使用 Object()。

【讨论】:

  • 谢谢,但您的代码会产生错误“预期类型”。
  • 改用New String()。
  • 是的,做到了。但丹尼尔需要编辑他的答案,拜托。
  • 您正在考虑 C# - VB 要么想要该类型,要么忽略“新”。您需要了解制作转换器的语言。
【解决方案2】:

正如 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    相关资源
    最近更新 更多