我试图找出如何搜索我的数组,然后找到我在原始文本框中显示的条目。
If idTextBox.Text = String.Empty Or nameTextBox.Text = String.Empty Then
MessageBox.Show("Please enter a valid name and/or ID number.")
Else
'retrieve user input & add to array
studentArray(0, columnId) = idTextBox.Text
studentArray(1, columnId) = nameTextBox.Text
studentArray(2, columnId) = majorComboBox.Text
studentArray(3, columnId) = yearComboBox.Text
If honorsRadioButton.Checked = True Then
studentArray(4, columnId) = "1"
ElseIf notHonorsRadioButton.Checked = True Then
studentArray(4, columnId) = "0"
End If
这是我的搜索和显示代码,当我搜索数组中不存在的ID时,它会崩溃。
Dim findstudentid As Integer
Dim didFindIt As Boolean = False
Integer.TryParse(idTextBox.Text, findstudentid)
If findstudentid <> 0 Then
Dim foundIt As Integer
Dim highColumn As Integer = studentArray.GetUpperBound(0)
'search each column of the first row
For columnID As Integer = 0 To highColumn
If findstudentid = CDbl(studentArray(0, columnID)) Then
foundIt = columnID
didFindIt = True
Exit For
End If
Next
If didFindIt Then
'read from array and write to interface
idTextBox.Text = studentArray(0, foundIt)
nameTextBox.Text = studentArray(1, foundIt)
Else
MessageBox.Show("Student data is not found. Please re-enter or add the student data!", "Student Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
idTextBox.Focus()
End If
Else
MessageBox.Show("Please enter a student id.", "Student Information", MessageBoxButtons.OK, MessageBoxIcon.Error)
idTextBox.Focus()
End If
答案 0 :(得分:2)
这是一个OOP方法的例子。使用此函数,您可以在调用它之前检查null / Nothing的返回值属性(会出错)。您甚至可以使用对象或List(Of T)
进行DataBinding。
Public Class Student
Public Property Id As Integer
Public Property Name As String
Public Property Major As String
Public Property Year As Integer
Public Property Honors As Boolean
End Class
以您的形式:
Public Class FrmMain : Inherits Form
Private Students As New List(Of Student)
Private Sub btnNewStudent(sender As Object, e As EventArgs) Handles btnNewStudent.Click
Dim _student As New Student
'fill the properties
Students.Add(_student)
End Sub
'...
End Class
Private Function FindStudent(Id As Integer) As Student
'return the student by Id or object with by Nothing
Return Students.Where(Function(s) s.Id = Id).FirstOrDefault
End Function
答案 1 :(得分:0)
我想通了
Dim findstudentid As Integer
Dim didFindIt As Boolean = False
Integer.TryParse(idTextBox.Text, findstudentid)
If findstudentid <> 0 Then
Dim foundIt As Integer
Dim highColumn As Integer = studentArray.GetUpperBound(0)
'search each column of the first row
For columnID As Integer = 0 To highColumn
If findstudentid = CDbl(studentArray(0, columnID)) Then
foundIt = columnID
didFindIt = True
Exit For
End If
Next
If didFindIt Then
'read from array and write to interface
idTextBox.Text = studentArray(0, foundIt)
nameTextBox.Text = studentArray(1, foundIt)
Else
MessageBox.Show("Student data is not found. Please re-enter or add the student data!", "Student Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
idTextBox.Focus()
End If
Else
MessageBox.Show("Please enter a student id.", "Student Information", MessageBoxButtons.OK, MessageBoxIcon.Error)
idTextBox.Focus()
End If