VB.NET:我找不到数组的索引

时间:2010-04-15 08:39:59

标签: vb.net arrays indexing reverse

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,找到与????

相关的索引的代码是什么

提前致谢

4 个答案:

答案 0 :(得分:1)

最好不要使用数组,而是查看字典对象。

外行术语中的字典与数组非常相似,但您可以使用键定位对象,在您的情况下是帐号。

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

答案 1 :(得分:0)

编写搜索功能,遍历数组并在找到指定记录后返回索引。或者您使用Dictionary,例如

  Public UserRecords As New Dictionary(Of Integer, Usersclass)

你可以像这样使用

  Dim desiredUser As Usersclass = UserRecords(hisAccountNumber)

答案 2 :(得分:0)

Dictionary对象可能正是您正在寻找的。它让你设置自己的密钥而不是使用数组索引。我写的VB不多,但这就是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];

答案 3 :(得分:0)

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