字符串' 1维数组的值'无法转换为' String'

时间:2013-10-04 20:05:45

标签: arrays vb.net

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

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()是无效的部分

3 个答案:

答案 0 :(得分:6)

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

Dim currentRow As String

而是需要将其声明为一维数组

Dim currentRow() as String

答案 1 :(得分:0)

您的currentRow变量未正确声明。而不是:

Dim currentRow As String

您需要以下其中一项:

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

您选择哪一个是优先考虑的问题。他们的意思是一样的。您也可以完全避免变量:

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

答案 2 :(得分:0)

在我的情况下,我声明我的方法错误地返回一个数组:

Function GetById(ByVal id As Integer) As String

,语法应为:

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

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

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