views:

167

answers:

4
This is the code for my array (which is working)

Public numUsers As Integer
Public fNameUsers As String = ("..\..\..\users.txt")
Public UserRecords As Usersclass() 'note... this line is in a module '

reader = New System.IO.StreamReader(fNameUsers)
numUsers = 0

   'Split the array up at each delimiter of "," and add new objects '
            Do While reader.Peek <> -1
                ReDim Preserve UserRecords(numUsers)
                oneline = reader.ReadLine
                fields = oneline.Split(",")
                UserRecords(numUsers) = New Usersclass
                UserRecords(numUsers).AccountNumber = fields(0)
                UserRecords(numUsers).CourseName = fields(1)
                UserRecords(numUsers).FirstName = fields(2)
                UserRecords(numUsers).LastName = fields(3)
                UserRecords(numUsers).DOB = fields(4)
                UserRecords(numUsers).Email = fields(5)
                UserRecords(numUsers).CourseProgress = (6)
                UserRecords(numUsers).AdminCheck = fields(7)

                numUsers = numUsers + 1

            Loop
            reader.Close()

My problem is that I don't know how to lookup the index of an array where the .accountNumber = a variable. For example the acccountNumber is 253, what is the code to find the index this relates to????

Thanks in advance

A: 

Write a search function, loop through the Array and return the index if you've found the specified record. Or you use a Dictionary, like

  Public UserRecords As New Dictionary(Of Integer, Usersclass)

which you could use like this

  Dim desiredUser As Usersclass = UserRecords(hisAccountNumber)
Bobby
I don't know what the code is to search through and find the index mate, that's my problem
steve
@steve: Do a simple `For i As Integer = 0 To userRecords.Length - 1 Step 1`-Loop with an `If`-Comparison and do `return i` if the account number matches.
Bobby
+1  A: 

You would be better off dropping the use of arrays and instead look at dictionary objects.

A dictionary in laymans terms is very similar to an array but you can locate an object using a key, in your case the account number.

             Dim UserRecords as  New Dictionary(Of String, Usersclass)
             Dim UserRecord as Userclass
             Do While reader.Peek <> -1

                oneline = reader.ReadLine
                fields = oneline.Split(",")

                'Populate your class
                UserRecord = New Usersclass
                UserRecord.AccountNumber = fields(0)
                UserRecord.CourseName = fields(1)
                UserRecord.FirstName = fields(2)
                UserRecord.LastName = fields(3)
                UserRecord.DOB = fields(4)
                UserRecord.Email = fields(5)
                UserRecord.CourseProgress = (6)
                UserRecord.AdminCheck = fields(7)

                'Add to the dictionary here
                UserRecords.Add (fields(0),UserRecord)

            Loop

            ''Then find your UserRecord by the accountnumber e.g
            UserRecord = UserRecords("253")
CResults
Hey, thanks for the fast reply. I'm not familiar with dictionary objects and I've got bunches of code everywhere. Any chance you could show me how to do it with my original way? I have no idea how to confidently navigate through and save, modify and delete files with this method
steve
Also is this finding the account number and not the index of the account number it relates to?
steve
You can do it your original way but it is inefficient - it would be like looking through every page of a book to find the page you are after. Why do you need to be able to find a user by index?
CResults
A: 

A Dictionary object is probably exactly what you're looking for. It let's you set you own key innstead of using an array index. I don't write much VB but here's what it would look like in c#:

//Create the dictionary
Dictionary<int, Account> userRecords = new Dictionary<int, Account>();

//Add an account to the dictionary
userRecords.Add( accountNumber, account );

//Get an accoutn out of the dictionary
Account account = userRecords[accountNumber];
Ben F
wow, I have no idea sorry... I've been programming for about a month
steve
A: 
Do While reader.Peek <> -1
                    ReDim Preserve UserRecords(numUsers)
                    oneline = reader.ReadLine
                    fields = oneline.Split(",")
                    UserRecords(numUsers) = New Usersclass
                    UserRecords(numUsers).AccountNumber = fields(0)
                    UserRecords(numUsers).CourseName = fields(1)
                    UserRecords(numUsers).FirstName = fields(2)
                    UserRecords(numUsers).LastName = fields(3)
                    UserRecords(numUsers).DOB = fields(4)
                    UserRecords(numUsers).Email = fields(5)
                    UserRecords(numUsers).CourseProgress = fields(6)
                    UserRecords(numUsers).AdminCheck = fields(7)
                    UserRecords(numUsers).password = fields(8)

                    numUsers = numUsers + 1

                Loop
steve