Tom Walker
Tom Walker

Reputation: 11

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

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

Upvotes: 1

Views: 38465

Answers (3)

JoshYates1980
JoshYates1980

Reputation: 3626

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

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415881

Your currentRow variable is not declared correctly. Instead of this:

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

Upvotes: 0

Steve
Steve

Reputation: 216323

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

Dim currentRow As String

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

Dim currentRow() as String

Upvotes: 6

Related Questions