954,152 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

TEXT file csvSTOCKS.TXT contains the following:
Stock 1,200,300,200,200
Stock 2,200,300,200,200
Stock 3,200,300,200,200
Stock 4,200,300,200,200
Stock 5,200,300,200,200

I'm attempting to read each line in the text file and put it in the array of strings lines()

Dim sr As StreamReader = New StreamReader("csvSTOCKS.TXT")

        Dim lines() As String

        Do While (sr.Peek <> -1)
            lines = sr.ReadLine
        Loop

        lstOutput.Items.Add(lines(0))


BUT, I get the following error when I try to assign lines = sr.ReadLine
ERROR:

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


If this cannot be done, is there a way to do that ?
tx

R3B3L
Newbie Poster
20 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

You have to redimension your lines array:

Dim sr As StreamReader = New StreamReader("csvSTOCKS.TXT")
        Dim lines() As String
        Dim index As Integer = 0

        Do While (sr.Peek <> -1)
            ReDim Preserve lines(index)
            lines(index) = sr.ReadLine
            index += 1
        Loop


or use an array list:

Dim sr As StreamReader = New StreamReader("csvSTOCKS.TXT")
        Dim lines As ArrayList = Nothing

        Do While (sr.Peek <> -1)
            lines.Add(sr.ReadLine)
        Loop
waynespangler
Posting Pro in Training
461 posts since Dec 2002
Reputation Points: 84
Solved Threads: 58
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You