I was investigating using custom business objects to represent my database data as opposed to datasets for my presentation level. Decided to try the typed custom collection thing. Anyway, here's my base test object:
Here is my typed collection:
... idea being, try developing a object add/delete control. The text box was used to enter new description, button1 is to add, button2 to remove the selected object in the listbox.
Everything works great except when I remove the last item in the listbox... then, instead of displaying my "Description", the listbox displays the default "ToString" and the listbox has no selected item. When I go to select an item in the listbox, I get :
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Specified argument was out of the range of valid values.
Anyway, I'm obviously missing something basic here. Until I understand, I really don't want to pursue this path.
Any help in explaining this behaviour would be GREATLY appreciated.
Thanks,
B.J.
Code:
Public Class TestObject
Private mID As Integer
Private mDesc As String
Public Sub New(ByVal id As Integer, ByVal desc As String)
mID = id
mDesc = desc
End Sub
Public ReadOnly Property ID() As Integer
Get
Return mID
End Get
End Property
Public ReadOnly Property Description() As String
Get
Return mDesc
End Get
End Property
End Class
Here is my typed collection:
Code:
Public Class TestCollection
Inherits CollectionBase
Public Sub New()
MyBase.New()
End Sub
Public Sub Add(ByVal testObj As TestObject)
MyBase.List.Add(testObj)
End Sub
Public Sub Remove(ByVal testObj As TestObject)
MyBase.List.Remove(testObj)
End Sub
End Class[\code]
Anyway, so I whipped up a test form:
[code]Public Class testform
Inherits System.Windows.Forms.Form
Private col As New TestCollection()
Private Shared count As Integer = -1
...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim testObj As New TestObject(count, TextBox1.Text)
col.Add(testObj)
initbox()
count -= 1
End Sub
Private Sub initbox()
ListBox1.DataSource = Nothing
ListBox1.DisplayMember = "Description"
ListBox1.DataSource = col
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
col.Remove(CType(ListBox1.SelectedItem, TestObject))
initbox()
End Sub
End Class
... idea being, try developing a object add/delete control. The text box was used to enter new description, button1 is to add, button2 to remove the selected object in the listbox.
Everything works great except when I remove the last item in the listbox... then, instead of displaying my "Description", the listbox displays the default "ToString" and the listbox has no selected item. When I go to select an item in the listbox, I get :
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Specified argument was out of the range of valid values.
Anyway, I'm obviously missing something basic here. Until I understand, I really don't want to pursue this path.
Any help in explaining this behaviour would be GREATLY appreciated.
Thanks,
B.J.