Solved

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

Posted on 2013-11-25
5
853 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39675695
0
 
LVL 1

Author Comment

by:iamtgo3
ID: 39675789
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 75

Accepted Solution

by:
käµfm³d   👽 earned 200 total points
ID: 39675830
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 1

Author Comment

by:iamtgo3
ID: 39678663
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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress

726 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