473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problem with custom class

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.SelectedIt em)
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
Nov 20 '05 #1
3 1104
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
"Andrew MacLean" <am******@sigem.com> wrote in message news:Oq**************@TK2MSFTNGP10.phx.gbl...
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.SelectedIt em)
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
Nov 20 '05 #2
Hello Andrew,

Change the offending line to:

itemList = cboFleetDispatch.Items(cboFleetDispatch.SelectedIt emIndex)
^^^^^

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

"Andrew MacLean" <am******@sigem.com> wrote in message
news:Oq**************@TK2MSFTNGP10.phx.gbl...
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.SelectedIt em)
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

Nov 20 '05 #3
That got it, Dmitriy,

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

Andrew MacLean
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: CGuy | last post by:
URGENT HELP REQUIRED FROM GURUS Hi, I have a custom object that implements ICollection and IListSource. This object has also an enumerator defined for it which implements IEnumerator and...
3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
3
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
2
by: Zach Mortensen | last post by:
I can't seem to get dynamically-compiled JScript code to use C#-defined custom attributes. I have a simple attribute and a class defined in a C# assembly: namespace MyNamespace { public...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
7
by: Nilesh | last post by:
I am using background-image attribute in a CSS file and linking the CSS file to aspx page. But strangly, background-image attribute is not working for relative URL. e.g. If I apply following css...
6
by: Vadivel Kumar | last post by:
I've a problem in handling a custom exception The following is my custom exception class: public class AppException : public Exception { public AppException (string message, Exception...
7
by: Girish | last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a huge learning experience.. My problem. I have a requirement to display a gird with a gird. Within the embedded grid,...
4
by: Jimmy | last post by:
hi, all I'm having a problem with creating custom events in wxpython. I have a class A handling some data processing work and another class B of GUI matter. I need GUI to display information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.