943,536 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 9495
  • VB.NET RSS
Sep 19th, 2008
0

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

Expand Post »
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()

vb Syntax (Toggle Plain Text)
  1. Dim sr As StreamReader = New StreamReader("csvSTOCKS.TXT")
  2.  
  3. Dim lines() As String
  4.  
  5. Do While (sr.Peek <> -1)
  6. lines = sr.ReadLine
  7. Loop
  8.  
  9. lstOutput.Items.Add(lines(0))

BUT, I get the following error when I try to assign lines = sr.ReadLine
ERROR:
VB.NET Syntax (Toggle Plain Text)
  1. 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
R3B3L is offline Offline
20 posts
since Mar 2008
Sep 19th, 2008
1

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

You have to redimension your lines array:
VB.NET Syntax (Toggle Plain Text)
  1. Dim sr As StreamReader = New StreamReader("csvSTOCKS.TXT")
  2. Dim lines() As String
  3. Dim index As Integer = 0
  4.  
  5. Do While (sr.Peek <> -1)
  6. ReDim Preserve lines(index)
  7. lines(index) = sr.ReadLine
  8. index += 1
  9. Loop

or use an array list:
VB.NET Syntax (Toggle Plain Text)
  1. Dim sr As StreamReader = New StreamReader("csvSTOCKS.TXT")
  2. Dim lines As ArrayList = Nothing
  3.  
  4. Do While (sr.Peek <> -1)
  5. lines.Add(sr.ReadLine)
  6. Loop
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Pass an arrayList to a function
Next Thread in VB.NET Forum Timeline: ScreenHeiht





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC