Problem with custom class

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
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hello Andrew,

Change the offending line to:

itemList = cboFleetDispatch.Items(cboFleetDispatch.SelectedItemIndex)
^^^^^

The Items property expects an integer index and you are passing a
clsListItem instance instead.
You can actually do the same thing simplier:

itemList = cboFleetDispatch.SelectedItem

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

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
 
O

One Handed Man

Hmm, I see what you're driving at. Please put OPTION STRICT to ON and then recompile the solution, this will tell you where you are potentially getting the error. As this is a runtime problem ( late binding ) Then you can either DirectCast it or take another action to remedy this.

PS : I suggest apsting Courier 10 Point instead of HTML to post your code.


OHM
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
 
A

Andrew MacLean

That got it, Dmitriy,

Actually, it was SelectedIndex, but you were right. Stupid mistake.
Thanks a million.

Andrew MacLean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top