Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Visual Basic Express 2008: need some help.
 

Visual Basic Express 2008: need some help.

I am new to writing code and have been trying to figure out a line of code for a project I am working on. I have a text file that the first part of my program writes data to, but for this second section, I need to read that file and use pieces of that data and average them out. The text file is setup up with 90 lines of data starting with the first 9 lines containing data and the 10th is a blank space. I need to read line 6, 8, and 9. I need to Loop through the file so I am using the Do While Loop. Where I am confused is when using the For...Next Loop to tell the program to read a specific line. If you have any info that can lead me to the answer that would be great. I would prefer you just point me in the right direction because I would very much like to somewhat figure it out in the process. At this point I am just lost and need some help.
Anobviousreason  Sunday, November 29, 2009 10:08 AM
It looks you have a small file, just read all lines and average them as necessary
You might have to check for whether file exists and also deciaml value are present
  Dim alllines = IO.File.ReadAllLines("textfile.txt")
        If alllines.Length > 8 Then
            Dim SixtValue As Decimal = Decimal.Parse(alllines(5))
            Dim EighthValue As Decimal = Decimal.Parse(alllines(7))
            Dim NinethValue As Decimal = Decimal.Parse(alllines(8))
            Dim Average = (SixtValue + EighthValue + NinethValue) / 3
            MessageBox.Show(Average.ToString)
Else
'Not a valid file
        End If



Arjun Paudel
Arjun Paudel  Sunday, November 29, 2009 12:48 PM
Hi,

Ifit's a case of you ALWAYS need to read lines 6, 8 and 9 and these lines will never change thencan do this by loading the file into an array and then accessing the array elements.
		Dim contents() as String = System.IO.File.ReadAllLines("path")
		Dim line6 as String = contents(5) 'zero based index (0 is line 1)
		Dim line8 as String = contents(7)
		Dim line9 as String = contents(8)

This isn't particularly efficient as the whole file is read in !

Derek Smyth  Sunday, November 29, 2009 12:51 PM
It looks you have a small file, just read all lines and average them as necessary
You might have to check for whether file exists and also deciaml value are present
  Dim alllines = IO.File.ReadAllLines("textfile.txt")
        If alllines.Length > 8 Then
            Dim SixtValue As Decimal = Decimal.Parse(alllines(5))
            Dim EighthValue As Decimal = Decimal.Parse(alllines(7))
            Dim NinethValue As Decimal = Decimal.Parse(alllines(8))
            Dim Average = (SixtValue + EighthValue + NinethValue) / 3
            MessageBox.Show(Average.ToString)
Else
'Not a valid file
        End If



Arjun Paudel
Arjun Paudel  Sunday, November 29, 2009 12:48 PM
Hi,

Ifit's a case of you ALWAYS need to read lines 6, 8 and 9 and these lines will never change thencan do this by loading the file into an array and then accessing the array elements.
		Dim contents() as String = System.IO.File.ReadAllLines("path")
		Dim line6 as String = contents(5) 'zero based index (0 is line 1)
		Dim line8 as String = contents(7)
		Dim line9 as String = contents(8)

This isn't particularly efficient as the whole file is read in !

Derek Smyth  Sunday, November 29, 2009 12:51 PM

You can use google to search for other answers

Custom Search

More Threads

• Timer & Some other...
• using insert statement in a for each ...next
• VisualBasic... missing reference
• Automatic logging into website and retrieving information from it
• Process.Start Probs
• Video stream from ip camera
• Update function problem: table adapter with multiple columns primary key
• window controls
• thread finished?
• Combobox Item Colors?