Solved

Read in comma delimited line value DAT file in VB.NET like you did in VB6

Posted on 2013-11-25
5
824 Views
Last Modified: 2013-11-26
I am looking to read in comma delimited line values in VB.NET The Dat file has various number of lines with carriage return at the end of each line. Each line has a total of 6 values per line. What I am looking to do is read the entire line in and parse the 6 values per line into their respected variables. Below are two examples VB6 and VB.NET.

Example Dat File (6 values per line)
1,2,3,4,5,6
we,are,3,2,1,me
Blue,Green,Red,Purple,Yellow,Black

VB6
Open Trim(sFile) For Input As #1
While Not EOF(1)
    Input #1, stLineValue(0), stLineValue(1), stLineValue(2), stLineValue(3), gGlobalValue, mModuleValue
Wend
Close #1

Open in new window


VB.NET (Something I found)
Dim iFieldNumber As Integer
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(stFileName)
    MyReader.TextFieldType = FileIO.FieldType.Delimited
    MyReader.SetDelimiters(",")
    Dim currentRow As String()
    While Not MyReader.EndOfData
        Try
			currentRow = MyReader.ReadFields()
			Dim currentField As String
			iFieldNumber = 0
			For Each currentField In currentRow
    			Select Case iFieldNumber
        			Case 0
						stLineValue(0) = currentField
        			Case 1
						stLineValue(1) = currentField
        			Case 2
						stLineValue(2) = currentField
        			Case 3
						stLineValue(3) = currentField
        			Case 4
						gGlobalValue = currentField
        			Case 5
						mModuleValue = currentField
    			End Select
    			iFieldNumber += 1
			Next
	Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
			MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
	End Try
	End While
End Using

Open in new window

0
Comment
Question by:iamtgo3
  • 2
  • 2
5 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
0
 
LVL 1

Author Comment

by:iamtgo3
Comment Utility
Kaufmed

Thanks for the reply but if you read above that is what I have already posted. In order to get each individual value I had to add a SELECT CASE

			iFieldNumber = 0
			For Each currentField In currentRow
    			Select Case iFieldNumber
        			Case 0
						stLineValue(0) = currentField
        			Case 1
						stLineValue(1) = currentField
        			Case 2
						stLineValue(2) = currentField
        			Case 3
						stLineValue(3) = currentField
        			Case 4
						gGlobalValue = currentField
        			Case 5
						mModuleValue = currentField
    			End Select
    			iFieldNumber += 1
			Next

Open in new window


I was hoping I could read in a line and then take all comma seperated values and assign them to variables without looping through 6 times for every line in the dat file.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 200 total points
Comment Utility
My apologies, I didn't inspect that second snippet that closely.

If you know for sure that you will always have 6 fields, then you can ditch the loop and hard-code the field indexes:

e.g.

While Not MyReader.EndOfData
    Try
        currentRow = MyReader.ReadFields()
        stLineValue(0) = currentRow(0)
        stLineValue(1) = currentRow(1)
        stLineValue(2) = currentRow(2)
        stLineValue(3) = currentRow(3)
        gGlobalValue = currentRow(4)
        mModuleValue = currentRow(5)
        Next
    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
    End Try
End While

Open in new window


...but you might encounter maintainability issues if the number of fields change--though to be honest you probably already have those issues.
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
0
 
LVL 1

Author Comment

by:iamtgo3
Comment Utility
Here it is all put together and I do agree with you on hard coding array size if it should change.

Working example #1
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(stFileName)
    MyReader.TextFieldType = FileIO.FieldType.Delimited
    MyReader.SetDelimiters(",")
	
	Dim stLineValue(4) As String
    Dim currentRow As String()
    While Not MyReader.EndOfData
        Try
			currentRow = MyReader.ReadFields()
			stLineValue(0) = currentRow(0)
			stLineValue(1) = currentRow(1)
			stLineValue(2) = currentRow(2)
			stLineValue(3) = currentRow(3)
			gGlobalValue = currentRow(4)
			mModuleValue = currentRow(5)
		Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
			MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
		End Try
	End While
End Using 

Open in new window


Working example #2
Dim MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(stFileName)
MyReader.Delimiters = New String() {","}
MyReader.HasFieldsEnclosedInQuotes = True
MyReader.TrimWhiteSpace = True
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited

Dim stLineValue(4) As String
Dim currentRow As String()
While Not MyReader.EndOfData
	Try
		currentRow = MyReader.ReadFields()
		stLineValue(0) = currentRow(0)
		stLineValue(1) = currentRow(1)
		stLineValue(2) = currentRow(2)
		stLineValue(3) = currentRow(3)
		gGlobalValue = currentRow(4)
		mModuleValue = currentRow(5)
	Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
		MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
	End Try
End While

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now