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

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 20
Reputation: R3B3L is an unknown quantity at this point 
Solved Threads: 0
R3B3L R3B3L is offline Offline
Newbie Poster

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

 
0
  #1
Sep 19th, 2008
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()

  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:
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

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

 
1
  #2
Sep 19th, 2008
You have to redimension your lines array:
  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:
  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
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC