|
-
Jan 6th, 2005, 05:23 AM
#1
Thread Starter
Frenzied Member
Invalid Cast Exception when Working with Inherited Classes
I know the title isn't exactly perfect, but the problem is hard to put it into words.
Basically what I have is two classes, they are almost perfectly the same, however the derrived class contains some extra elements such as in this example.
VB Code:
'Base Class
Public Class CarBase
Protected _Colour As String
Protected _Model As String
Protected _Make As String
Public Property Colour() As String
Get
Return _Colour
End Get
Set(ByVal Value As String)
_Colour = Value
Call ValueChanged()
End Set
End Property
'
'Property Repeated for the Model and the Make
Protected Overridable Sub ValueChanged()
'To be utilised in derrived classes
End Sub
End Class
'Derrived Class
Public Class Car
Inherits CarBase
Private _ChangesMade As Boolean = False
Protected Overrides Sub ValueChanged()
_ChangesMade = True
End Sub
Public ReadOnly Property ChangesMade() As Boolean
Get
Return _ChangesMade
End Get
End Property
End Class
Now, the problem is that i have other functions and stuff to manipulate the classes, they all work from the base class (CarBase) because that stores just the data items, nothing to manipulate them etc. The derrived class really only adds the ChangesMade and some other properties that don't really affect the stored data.
I need both classes to be able to access the functions, but I don't really want to have to rewrite them to accomodate the two types of classes, some of which only return values so I can't overload them.
When I try to assign a CarBase to a variable of type Car, I get an Invalid Cast Exception. Is there anyway I can allow it to do this type of action w/o raising an exception? The IDE doesn't detect an error and neither does the compiler, so there must not be anything wrong with the syntax.
Last edited by Ideas Man; Jan 7th, 2005 at 08:22 AM.
I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|