I've been searching around these forums for quite a while now, and can't seem to either understand or find what I'm looking for.

^ As you can probably tell, I'm a real Novice to VB!

I have a text file, that my program is always adding to it, it's format is like so:

Line 1: UserName (For User 1)
Line 2: FilePath (For User 1)
Line 3: UserName (For User 2)
Line 4: FilePath (for User 2)

For Example:

Line 1: Tobyjug2222
Line 2: J:\Program name\bin\Debug\Accounts\Tobyjug2222
Line 3: Toby
Line 4: J:\Program name\bin\Debug\Accounts\Toby

My program automatically writes this to the file when an account is created, and there for I don't know what my Upper bound is, as It's always changing.

How can I write this to an array and read it as Line 1, and Line 2 (Or Line 3 and Line 4) are related?

I hope I've added all the information needed,

Thanks, Toby.

(P.S. This is related to my other thread : http://www.daniweb.com/software-development/vbnet/threads/398774/1709547#post1709547 )

Recommended Answers

All 8 Replies

Does it REALLY need to be an array?
Would a List of classes or List of KeyValuePairs work?

Yes, Sure.. I thought that an Array would be easiest, but anything really.

I know "array" sounds easier to use, but I find they require more management than the collection classes.
I know some of these other things look hard to use at first, but if you think about it: without them, you're doing all of the work they will do for you.

I would start out with something like a List of Key/Value pairs.
The Key (user name) is on the left and the Value (path) is on the right.
...and store those in a list. My list would be named like this:

Dim lst_kvp_s2sNamePath As New List(Of KeyValuePair(Of String, String))

I would then
1) Open the text file as a StreamReader
2) Loop through the file with TWO ReadLine calls per pass (one for name, one for the path)
3) Simultaneously add the key and value to the key/value pair and to the list with:

lst_kvp_s2sNamePath.Add(New KeyValuePair(Of String, String)(strName, strPath))

If later you need to see the contents of the list, you can loop through it with a For Each

Let me know if that's confusing.

If the Name-ID things are guaranteed to be unique, you can use a Dictionary(String, String).
That might make it less cryptic.
I used the KVP because it does not require the individual keys to be unique.

Yes - Each Username and Path will be Unique.

I'm trying out your method now, I seem slightly lost, but I will see if I can understand it or not :)

.see if this helps.

Imports System.IO
Public Class Form1
    Private myFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\test.txt"
    Private arFileLines() As String = Nothing

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    End Sub
    Private Sub myCoolSub()
        If File.Exists(myFile) Then
            arFileLines = File.ReadAllLines(myFile)
            For i As Integer = 0 To arFileLines.Length - 1 Step +2 '// loop thru file.lines and skip reading every other line.
                MsgBox(arFileLines(i) & vbNewLine & arFileLines(i + 1))
        End If
    End Sub
End Class

Thanks a lot both of you, I used thines01's method, although thanks for the contribution codeorder :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.