By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,594 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,594 IT Pros & Developers. It's quick & easy.

Trying to input data (double) from text file to array

P: 2
Hi All, I'm trying to read data from a text file into an array. The text file (a test) looks like this:

1
2
3
4
5
6
7
8
9

And the code I've got so far looks like this:

Dim readerVar As StreamReader
Dim dr As DialogResult
Dim i As Integer
Dim WorkString As String = "a"

dr = dlgOpen.ShowDialog()
If dr = DialogResult.OK Then

readerVar = New StreamReader(dlgOpen.FileName)
i = 0
While readerVar.Peek <> -1 ' Loop to read number of lines in file
WorkString = readerVar.ReadLine() ' Get line
i = i + 1 ' Count number of lines in file
End While

ReDim readArray(i)
readerVar = New StreamReader(dlgOpen.FileName)
i = 0
While readerVar.Peek <> -1
WorkString = readerVar.ReadLine() ' Get line
readArray(i) = Double.TryParse(WorkString, readArray(i))

i = i + 1
End While
txtBox.Text = readArray(0)
readerVar.Close()
Else

End If

(I know, terrible isn't it!)

The array now reads something like this:

-1
-1
-1
-1
-1
-1
-1
0
0

Can anyone help me?

Andy

PS does anyone have any code to draw a simple graph from a 1D array?
Oct 31 '06 #1
Share this Question
Share on Google+
2 Replies


100+
P: 1,646
Hi All, I'm trying to read data from a text file into an array. The text file (a test) looks like this:

1
2
3
4
5
6
7
8
9

And the code I've got so far looks like this:

Dim readerVar As StreamReader
Dim dr As DialogResult
Dim i As Integer
Dim WorkString As String = "a"

dr = dlgOpen.ShowDialog()
If dr = DialogResult.OK Then

readerVar = New StreamReader(dlgOpen.FileName)
i = 0
While readerVar.Peek <> -1 ' Loop to read number of lines in file
WorkString = readerVar.ReadLine() ' Get line
i = i + 1 ' Count number of lines in file
End While

ReDim readArray(i)
readerVar = New StreamReader(dlgOpen.FileName)
i = 0
While readerVar.Peek <> -1
WorkString = readerVar.ReadLine() ' Get line
readArray(i) = Double.TryParse(WorkString, readArray(i))

i = i + 1
End While
txtBox.Text = readArray(0)
readerVar.Close()
Else

End If

(I know, terrible isn't it!)

The array now reads something like this:

-1
-1
-1
-1
-1
-1
-1
0
0

Can anyone help me?

Andy

PS does anyone have any code to draw a simple graph from a 1D array?
Hi Andy,
This looks like .NET code. Not too easy for crusty old vb6 monkeys to help :)
Oct 31 '06 #2

Expert 5K+
P: 8,434
Hi Andy,
This looks like .NET code. Not too easy for crusty old vb6 monkeys to help :)
Afraid I'm VB6 too, but could it be related to not closing the reader after the first time through the file? I'm not familiar with the Double.TryParse business, guess I would have used Val() or Cdbl() functions.

Also, on closer inspection, even without really being familiar with .NET, this construction
Expand|Select|Wrap|Line Numbers
  1. readArray(i) = Double.TryParse(WorkString, readArray(i))
just doesn't look correct. readArray(i) is both one of the two parameters, and the output. Perhaps the value returned by the TryParse method (-1 or 0 in this case) is simply a success/failure indicator. Maybe try putting that in a dummy variable. Or, if it is valid syntax, just do this
Expand|Select|Wrap|Line Numbers
  1. Double.TryParse(WorkString, readArray(i))
Oct 31 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.