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 UserReco开发者_如何学JAVArds(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
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")
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)
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];
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
精彩评论