A
Andrew MacLean
Hello all,
I am fairly new to .NET, though highly experienced in older versions of VB. I am having a problem with a class I created, though.
I have created a class called clsListItem based upon sample code in an attempt to replicate the ItemData property in a ComboBox:
Public Class clsListItem
Private sText As String
Private iData As Integer
Public Sub New()
sText = ""
iData = 0
End Sub
Public Sub New(ByVal Text As String, ByVal Data As Integer)
sText = Text
iData = Data
End Sub
Public Property Name() As String
Get
Return sText
End Get
Set(ByVal sValue As String)
sText = sValue
End Set
End Property
Public Property ItemData() As Integer
Get
Return iData
End Get
Set(ByVal iValue As Integer)
iData = iValue
End Set
End Property
Public Overrides Function ToString() As String
Return sText
End Function
End Class
I use the following code to add items to the ComboBox:
objComboBox.Items.Add(New clsListItem(sqlModeRow("Description"), sqlModeRow("Dispatch Mode")))
which works fine.
However, when I try to retrieve the item from thr ComboBox as follows:
Private Sub cboFleetDispatch_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cboFleetDispatch.SelectedIndexChanged
Dim itemList As clsListItem
itemList = cboFleetDispatch.Items(cboFleetDispatch.SelectedItem)
End Sub
I receive the following error:
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from type 'clsListItem' to type 'Integer' is not valid.
When executing the itemList line. I've tried everything, but I have correctly declared the type and should be able to populate it with the same type. Any ideas out there? Sorry about the HTML, it was the only way I could get this to line up properly.
Thanks,
Andrew MacLean
I am fairly new to .NET, though highly experienced in older versions of VB. I am having a problem with a class I created, though.
I have created a class called clsListItem based upon sample code in an attempt to replicate the ItemData property in a ComboBox:
Public Class clsListItem
Private sText As String
Private iData As Integer
Public Sub New()
sText = ""
iData = 0
End Sub
Public Sub New(ByVal Text As String, ByVal Data As Integer)
sText = Text
iData = Data
End Sub
Public Property Name() As String
Get
Return sText
End Get
Set(ByVal sValue As String)
sText = sValue
End Set
End Property
Public Property ItemData() As Integer
Get
Return iData
End Get
Set(ByVal iValue As Integer)
iData = iValue
End Set
End Property
Public Overrides Function ToString() As String
Return sText
End Function
End Class
I use the following code to add items to the ComboBox:
objComboBox.Items.Add(New clsListItem(sqlModeRow("Description"), sqlModeRow("Dispatch Mode")))
which works fine.
However, when I try to retrieve the item from thr ComboBox as follows:
Private Sub cboFleetDispatch_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cboFleetDispatch.SelectedIndexChanged
Dim itemList As clsListItem
itemList = cboFleetDispatch.Items(cboFleetDispatch.SelectedItem)
End Sub
I receive the following error:
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from type 'clsListItem' to type 'Integer' is not valid.
When executing the itemList line. I've tried everything, but I have correctly declared the type and should be able to populate it with the same type. Any ideas out there? Sorry about the HTML, it was the only way I could get this to line up properly.
Thanks,
Andrew MacLean