2
\$\begingroup\$

I am needing to import a tab delimited text file that has 11 columns and an unknown number of rows (always minimum 3 rows).

I would like to import this text file as an array and be able to call data from it as needed, throughout my project. And then, to make things more difficult, I need to replace items in the array, and even add more rows to it as the project goes on (all at runtime).

Hopefully someone can suggest code corrections or useful methods. I'm hoping to use something like the array style sMyStrings(3,2), which I believe would be the easiest way to control my data.

Imports System.IO
Imports Microsoft.VisualBasic.FileIO
Public Class Main
 Dim strReadLine As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 Dim sReader As IO.StreamReader = Nothing
 Dim sRawString As String = Nothing
 Dim sMyStrings() As String = Nothing
 Dim intCount As Integer = -1
 Dim intFullLoop As Integer = 0
 If IO.File.Exists("C:\MyProject\Hardware.txt") Then ' Make sure the file exists
 sReader = New IO.StreamReader("C:\MyProject\Hardware.txt")
 Else
 MsgBox("File doesn't exist.", MsgBoxStyle.Critical, "Error")
 End
 End If
 Do While sReader.Peek >= 0 ' Make sure you can read beyond the current position
 sRawString = sReader.ReadLine() ' Read the current line
 sMyStrings = sRawString.Split(New Char() {Chr(9)}) ' Separate values and store in a string array
 For Each s As String In sMyStrings ' Loop through the string array
 intCount = intCount + 1 ' Increment
 If TextBox1.Text <> "" Then TextBox1.Text = TextBox1.Text & vbCrLf ' Add line feed
 TextBox1.Text = TextBox1.Text & s ' Add line to debug textbox
 If intFullLoop > 14 And intCount > -1 And CBool((intCount - 0) / 11 Mod 0) Then
 cmbSelectHinge.Items.Add(sMyStrings(intCount))
 End If
 Next
 intCount = -1
 intFullLoop = intFullLoop + 1
 Loop
End Sub
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jun 10, 2014 at 5:17
\$\endgroup\$
0

2 Answers 2

3
\$\begingroup\$

First I'd like to mention a style point. Ditch the hungarian notation. You don't need it. It often isn't maintained, so they tend to lie. when they are, it's a bit of a pain to find/replace your variable names. sReader should just be reader, intCount should just be count and so on.

I like that you're taking advantage of the single line variable declaration & assignment.

You should be using a few constants for your data file though. This reduces code duplication and makes it easier to change down the road.

const dataFilePath As String = "C:\MyProject\"
const dataFileName As String = "Hardware.txt"
const dataFile As String = dataFilePath & dataFileName

Now your code would look like this.

If IO.File.Exists(dataFile) Then ' Make sure the file exists
 reader = New IO.StreamReader(dataFile)
Else
 '....

I don't see anything inherently wrong about your method of processing the file line by line. It's essentially no different from working in a 2 dimensional array. You may want to find out if you can read the whole file at once and use one anyway. I bet there would be a performance benefit.

What I do notice about your loops is the heavy reliance on Magic Numbers.

Go ahead and declare another constant TabChar and set it equal to Chr(9). And here:

 If intFullLoop > 14 And intCount > -1 And CBool((intCount - 0) / 11 Mod 0) Then
 cmbSelectHinge.Items.Add(sMyStrings(intCount))
 End If

What is 14? What is 11? Find appropriate names for them. It makes your code easier to read and easier to change/maintain later.

Further Reading on Naming Conventions

answered Jun 10, 2014 at 13:18
\$\endgroup\$
1
\$\begingroup\$

One way to hold all your data is with a DataTable. This gives you a collection of rows and each row has an ItemArray collection. Here's and example from a different post:

Dim sr As New IO.StreamReader(filename)
Dim dt As New DataTable
Dim newline() As String = sr.ReadLine.Split(","c)
dt.Columns.AddRange({New DataColumn(newline(0)), _
 New DataColumn(newline(1))})
While (Not sr.EndOfStream)
 newline = sr.ReadLine.Split(","c)
 Dim newrow As DataRow = dt.NewRow
 newrow.ItemArray = {newline(0), newline(1)}
 dt.Rows.Add(newrow)
End While
DataGridView1.DataSource = dt
answered Jun 10, 2014 at 14:51
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.