nickbtheitguy
Member
- Joined
- Feb 21, 2009
- Messages
- 6
- Programming Experience
- Beginner
I am new to VB.net and I am trying to work out an exercise from a book I am reading on VB. Basically I am storing some book information in a hashtable using a class called Book to store the book information. I am trying to work out my Search and Display All procedures. My Search will work for the first value in my arraylist but not for any other values. I even added the ISBN to my book class to try and use this as a search point. The ISBN is used to pull the item from the hashtable as well.
In the main code I am having problems with the ButtonSearch and ButtonAll. Any suggestions would be great.
I have attached what my form looks like.
Main Code
Book.vb Class
In the main code I am having problems with the ButtonSearch and ButtonAll. Any suggestions would be great.
I have attached what my form looks like.
Main Code
VB.NET:
Public Class FormMain
Private bookTable As Hashtable = New Hashtable
Private bookArray As New ArrayList
Private Sub clearTextBoxes()
TextBoxISBN.Text = String.Empty
TextBoxTitle.Text = String.Empty
TextBoxYear.Text = String.Empty
TextBoxAuthor.Text = String.Empty
End Sub
Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
Dim aBook As New Book(TextBoxISBN.Text, TextBoxTitle.Text, TextBoxYear.Text, TextBoxAuthor.Text)
bookArray.Add(aBook.ISBN)
bookArray.Add(aBook.Title)
bookArray.Add(aBook.Year)
bookArray.Add(aBook.Author)
bookTable.Add(aBook.ISBN, bookArray)
MessageBox.Show(aBook.ToString, "Added")
clearTextBoxes()
End Sub
Private Sub ButtonSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSearch.Click
Dim foundBook As Object = bookTable.Item(TextBoxISBN.Text)
If Not foundBook Is Nothing Then
Try
Dim searchArray As Integer
Dim searchOutput As String
searchArray = 0
searchArray = bookArray.BinarySearch(TextBoxISBN.Text, New CaseInsensitiveComparer())
If searchArray > 0 Then
'searchOutput = bookArray(searchArray - 1)
MessageBox.Show("Found at " & searchArray)
Else
MessageBox.Show("Not Found")
End If
Catch ex As Exception
MessageBox.Show("Book not found", "Search")
End Try
End If
clearTextBoxes()
End Sub
Private Sub ButtonRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRemove.Click
Dim foundBook As Object = bookTable.Item(TextBoxISBN.Text)
If Not foundBook Is Nothing Then
bookTable.Remove(TextBoxISBN.Text)
MessageBox.Show(foundBook.ToString, "Remove")
Else
MessageBox.Show("Book not found", "Remove")
End If
clearTextBoxes()
End Sub
Private Sub ButtonAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAll.Click
Dim element As DictionaryEntry
Dim output As String = Nothing
For Each element In bookTable
output &= element.Key & ControlChars.Tab & element.Value + vbCrLf
Next
MessageBox.Show(output, "Books")
End Sub
Private Sub ButtonExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
End Class
Book.vb Class
VB.NET:
Public Class Book
' instance variables
Private _ISBN As String
Private _title As String
Private _year As String
Private _author As String
Public Sub New(ByVal myISBN As String, ByVal myTitle As String, ByVal myYear As String, ByVal myAuthor As String)
_ISBN = myISBN
_title = myTitle
_year = myYear
_author = myAuthor
End Sub
Public Property ISBN() As String
Get
Return _ISBN
End Get
Set(ByVal value As String)
_ISBN = value
End Set
End Property
Public Property Title() As String
Get
Return _title
End Get
Set(ByVal value As String)
_title = value
End Set
End Property
Public Property Year() As String
Get
Return _year
End Get
Set(ByVal value As String)
_year = value
End Set
End Property
Public Property Author() As String
Get
Return _author
End Get
Set(ByVal value As String)
_author = value
End Set
End Property
Public Overrides Function ToString() As String
Return _ISBN & " " & _title & " " & _year & " " & _author
End Function
End Class
Attachments
Last edited: