vb.net “字符串的一维数组”类型的值无法转换为“字符串”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19189860/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:11:51  来源:igfitidea点击:

Value of type '1-dimensional array of String' cannot be converted to 'String'

arraysvb.net

提问by Tom Walker

I've been coding a Film Search thing for School, and I've came across this problem and I'm not sure how to solve it.

我一直在为学校编写电影搜索程序,但遇到了这个问题,但我不知道如何解决。

While Not FilmSearcher.EndOfData         'loop until end of file
    currentRow = FilmSearcher.ReadFields()   'read in the fields of each line
    For j = 0 To 3
        Films(i, j) = currentRow(j)      'put the fields into film array
    Next

    i = i + 1
End While

currentRow = FilmSearcher.ReadFields() is the part that isn't working

currentRow = FilmSearcher.ReadFields() 是不起作用的部分

回答by Steve

If the error message is in your title, then I bet you have declared the currentRow as

如果错误消息在您的标题中,那么我敢打赌您已将 currentRow 声明为

Dim currentRow As String

instead you need to declare it as a 1-dimensional array

相反,您需要将其声明为一维数组

Dim currentRow() as String

回答by Joel Coehoorn

Your currentRowvariable is not declared correctly. Instead of this:

您的currentRow变量未正确声明。取而代之的是:

Dim currentRow As String

You need one of these:

您需要其中之一:

Dim currentRow() As String
Dim currentRow As String()

Which one you choose is a matter of preference. They mean the same thing. You can also avoid the variable entirely:

你选择哪一个是一个偏好问题。他们的意思是一样的。您还可以完全避免使用该变量:

While Not FilmSearcher.EndOfData        
    'read in the fields of each line and put into film array
    Films(i) = FilmSearcher.ReadFields()          
    i = i + 1
End While

回答by JoshYates1980

In my case, I had declared my method incorrectly to return an array:

就我而言,我错误地声明了我的方法以返回一个数组:

Function GetById(ByVal id As Integer) As String

and the syntax should be:

语法应该是:

Function GetById(ByVal id As Integer) As String()

whereas I can return an array of strings:

而我可以返回一个字符串数组:

Dim arrayOfStrings() As String
arrayOfStrings = {"John", "Josh", "Jason", "Jill", "Kunta Kinte"}
Return arrayOfStrings