473,809 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Ite ms.Add(New clsListItem(sql ModeRow("Descri ption"), sqlModeRow("Dis patch Mode")))

which works fine.

However, when I try to retrieve the item from thr ComboBox as follows:

Private Sub cboFleetDispatc h_SelectedIndex Changed(ByVal sender As System.Object, _
ByVal e As System.EventArg s) Handles cboFleetDispatc h.SelectedIndex Changed
Dim itemList As clsListItem
itemList = cboFleetDispatc h.Items(cboFlee tDispatch.Selec tedItem)
End Sub
I receive the following error:

An unhandled exception of type 'System.Invalid CastException' occurred in microsoft.visua lbasic.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 1120
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******** ******@TK2MSFTN GP10.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.Ite ms.Add(New clsListItem(sql ModeRow("Descri ption"), sqlModeRow("Dis patch Mode")))

which works fine.

However, when I try to retrieve the item from thr ComboBox as follows:

Private Sub cboFleetDispatc h_SelectedIndex Changed(ByVal sender As System.Object, _
ByVal e As System.EventArg s) Handles cboFleetDispatc h.SelectedIndex Changed
Dim itemList As clsListItem
itemList = cboFleetDispatc h.Items(cboFlee tDispatch.Selec tedItem)
End Sub
I receive the following error:

An unhandled exception of type 'System.Invalid CastException' occurred in microsoft.visua lbasic.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 = cboFleetDispatc h.Items(cboFlee tDispatch.Selec tedItemIndex)
^^^^^

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

itemList = cboFleetDispatc h.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******** ******@TK2MSFTN GP10.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.Ite ms.Add(New clsListItem(sql ModeRow("Descri ption"),
sqlModeRow("Dis patch Mode")))

which works fine.

However, when I try to retrieve the item from thr ComboBox as follows:

Private Sub cboFleetDispatc h_SelectedIndex Changed(ByVal sender As
System.Object, _
ByVal e As System.EventArg s) Handles
cboFleetDispatc h.SelectedIndex Changed
Dim itemList As clsListItem
itemList = cboFleetDispatc h.Items(cboFlee tDispatch.Selec tedItem)
End Sub
I receive the following error:
An unhandled exception of type 'System.Invalid CastException' occurred in
microsoft.visua lbasic.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
2849
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 IList. Now, I'm trying to bind object to a combo box. The bidning happens correctly, but I'm unable to set the DisplayMember property of the combo box (the count of items in the combo box gives me the correct value, but no items are displayed in the...
3
3154
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 from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
3
8121
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 run the class it gives an error that "System.Xml.XmlNode.XmlNode() is inaccessible due to its protection level". This error comes because XmlNode has not any public constructor. I found XmlNode has two constructor but both are private or friend...
2
2386
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 abstract class MyCsharpAttribute : Attribute { }
2
2520
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 pointer or reference to a derived type of the base class's return type. In .NET the overridden virtual function is similar, but an actual parameter of the function can be a derived reference from the base class's reference also. This dichotomy...
6
3263
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 query-string flag is used to indicate to the page whether it should instantiate a new instance of the object or access an existing instance from the calling page. On the both pages I have a property of the page which is an instance of this custom...
7
5131
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 ..navbar-background { background-image: url(images/menubar.gif); } the image is not appearing on the page. It seems that IE is picking
6
1766
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 innerException) { } public override string Message
7
3348
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, theres a requirement to show a drop down menu list (this is a control I downloaded online) in one of the columns. For the purposes of this question, Ive implemented the drop down menu as a drop down list instead. Ive got all this working at this...
4
2853
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 when data in A is updated. I know cutom events in wxpython may work. But I found no material paricularly helpful :(
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10387
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.