Reputation: 49
I'm trying to figure out how I can search my array and then have the entry I'm looking for displayed in the original textbox.
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
This is the search and display code I have, it crashes when I search for an ID not present in the array.
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
Upvotes: 0
Views: 349
Reputation: 9024
Here is an example of an OOP way to do it. With this function you can check the return value for a null/Nothing found before calling it's properties(would error). You can even do DataBinding with the object or it's List(Of T)
.
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
In your form:
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
Upvotes: 2
Reputation: 49
I figured it out
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
Upvotes: 0