Jump to content

[vb.net] Arrays and textfiles


Stormfolk
 Share

Recommended Posts

I'm working on exercises about vb.net arrays. In this exercise I have to read in some data from a .txt file and place it in a 1 dimensional array. It has to do some other things later too, but that's not important atm. Basically, it is a text file with names and a result.

 

The textfile looks like this:

 

99020860.png

As you can see the data is seperated by a "-".

 

I managed to get it all working with this code:

 

Public Class Form1

   Private Sub btnSluiten_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSluiten.Click
       Me.Close()
   End Sub

   Private Sub btnInlezen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInlezen.Click
       'declare
       Dim record As String
       Dim index As Integer = -1
       Dim position As Integer

       Dim name() As String
       Dim result_a() As Integer

       ‘The textfile
       FileOpen(1, "gegevens.txt", OpenMode.Input) 'standaardwaarde inlezen is 1

       'read data from the textfile
       Do While Not EOF(1)
           'raise index
           index += 1

           'read lines of the textbox
           record = LineInput(1)

           'preserve 
           ReDim Preserve name(index)
           ReDim Preserve result_a(index)

           'seperate the data in the txt
           position = record.IndexOf("-")

           'fill the arrays
           name(index) = record.Substring(0, position)
           result_a(index) = record.Substring(position + 1)

           'show array
           txtData.Text += name(index).ToString & vbTab & result_a(index).ToString & vbNewLine
       Loop

       ‘close txt
       FileClose(1)
   End Sub
End Class

 

How the program looks like after the text has been read:

65928783.png

 

 

 

So far that's all ok, but I wanted to go a little bit further. In stead of 2 pieces of data on each line of the textfile, I wanted to make it work for when there's 3, 4, 5, ... things. Now there's just a result_a, but what if there's a result_b, result_c, ...

 

For example:

 

Mia-75-53-82-65

Jan-85-94-78-66

 

I've been trying and testing things for most of the day, looked things up on the internet, ... but somehow I'm missing the point on how to get this done. I couldn't find a proper explanation for it either.

 

I tried to do something like, but apperently that was too easy to work :P :

 

result_a(index) = record.Substring(position + 1)
result_b(index) = record.Substring(position + 2)
result_c(index) = record.Substring(position + 3)

 

But then something goes wrong with the splitting based on the "-" symbol. It seems to fail to jump further and get the next piece of data in the line.

 

I also tried adding in this again before I moved to the result_b and after I read in result a, but that didn't do the trick either.

 

            position = record.IndexOf("-")

 

Anyone here has an idea, and preferably an explanation on how to do this for a name and 2,3,4, ... results? I'm hoping I can find a general theory or a general way so I can use this basic code to read in files with few data, but also files with more data. So yea, if you can help this vb noob it would be greatly appreciated.

tattoov2.png

 

Stormfolk | TRWF Clanfriend

 

Link to comment
Share on other sites

.

rnHJQwZ.png
In a state of tranquillity, wealth, and luxury, our descendants would forget the arts of war and the noble activity and zeal which made their ancestors invincible. ~Samuel Adams; 1 August, 1776
There are men, in all ages, who mean to exercise power usefully; but who mean to exercise it. They mean to govern well; but they mean to govern. They promise to be kind masters; but they mean to be masters. ~Daniel Webster; 15 March, 1837

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.