Stormfolk Posted January 6, 2012 Share Posted January 6, 2012 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: 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: 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-65Jan-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. Stormfolk | TRWF Clanfriend Link to comment Share on other sites More sharing options...
Stormfolk Posted January 8, 2012 Author Share Posted January 8, 2012 A little bump Stormfolk | TRWF Clanfriend Link to comment Share on other sites More sharing options...
Ember Posted January 8, 2012 Share Posted January 8, 2012 . Link to comment Share on other sites More sharing options...
Stormfolk Posted January 8, 2012 Author Share Posted January 8, 2012 Thanks, that seems to have done the trick. I'm gonna test it some more and then I'll be trying 2D arrays lol :D Stormfolk | TRWF Clanfriend Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now