Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-11-25
5
Medium Priority
?
869 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 800 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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…
Starting up a Project

609 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