一千萬個為什麽

搜索

VB.NET:我找不到數組的索引

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()

我的問題是我不知道如何查找.accountNumber =變量的數組的索引。例如,acccountNumber是253,找到與之相關的索引的代碼是什麽?

提前致謝

最佳答案

你最好放棄使用數組,而是查看字典對象。

外行術語中的字典與數組非常相似,但您可以使用鍵定位對象,在您的情況下是帳號。

             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")

轉載註明原文: VB.NET:我找不到數組的索引