vb.net “字符串的一维数组”类型的值无法转换为“字符串”。(BC30311)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13112943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Value of type '1-dimensional array of String' cannot be converted to 'String'. (BC30311)
提问by Ommy Naituivau
I have this code which gives an error:
我有这个代码,它给出了一个错误:
'declaration
Dim strFieldValues As String
'split
strFieldValues = strRecord.Split(",") 'field are separated by commas
回答by Jon Skeet
Well, the error seems to be pretty self-explanatory to me. You've declared a variable of type String- i.e. it can hold a value of a single Stringreference:
好吧,这个错误对我来说似乎是不言自明的。您已经声明了一个类型的变量String- 即它可以保存单个String引用的值:
Dim strFieldValues As String
You've then tried to assign a value to it returned from String.Split():
然后,您尝试为从 返回的值分配一个值String.Split():
strFieldValues = strRecord.Split(",")
Now String.Split()returns a Stringarray, not a single string value.
现在String.Split()返回一个String数组,而不是单个字符串值。
So you have two courses of action open to you:
因此,您有两个行动方案可供您选择:
- Change
strFieldValuesto an array variable - Change the value you assign to it
- 更改
strFieldValues为数组变量 - 更改您分配给它的值
My guess is that you want the first, but we don't know what you're trying to achieve. The simplest approach would be to combine declaration and initialization:
我的猜测是您想要第一个,但我们不知道您想要实现什么。最简单的方法是结合声明和初始化:
Dim strFieldValues = strRecord.Split(",")
You may also need to change the arguments to Split- I don't know how VB will sort out that call.
您可能还需要将参数更改为Split- 我不知道 VB 将如何整理该调用。
回答by Saul Delgado
If all you want to do is just retrieve either side of the resulting string array, you could just invoke the left or right part like this:
如果您只想检索结果字符串数组的任一侧,您可以像这样调用左侧或右侧部分:
strFieldValues = strRecord.Split(",")(0) ' Text to the left of the delimiter character
Or
或者
strFieldValues = strRecord.Split(",")(1) ' Text to the right of the delimiter character
Of course, this assumes that the delimiter character does exist, so you should take the necessary precautions to ensure that you won't run into a runtime exception if said character is not found on the string you are splitting.
当然,这假设定界符确实存在,因此您应该采取必要的预防措施,以确保如果在要拆分的字符串中找不到该字符,则不会遇到运行时异常。

