Results 1 to 12 of 12

Thread: Invalid Cast Exception when Working with Inherited Classes

Threaded View

  1. #1

    Thread Starter
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718

    Resolved 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:
    1. 'Base Class
    2. Public Class CarBase
    3.  
    4.     Protected _Colour As String
    5.     Protected _Model As String
    6.     Protected _Make As String
    7.  
    8.     Public Property Colour() As String
    9.         Get
    10.             Return _Colour
    11.         End Get
    12.         Set(ByVal Value As String)
    13.             _Colour = Value
    14.             Call ValueChanged()
    15.         End Set
    16.     End Property
    17.     '
    18.     'Property Repeated for the Model and the Make
    19.  
    20.     Protected Overridable Sub ValueChanged()
    21.         'To be utilised in derrived classes
    22.     End Sub
    23.  
    24. End Class
    25.  
    26. 'Derrived Class
    27. Public Class Car
    28.     Inherits CarBase
    29.  
    30.     Private _ChangesMade As Boolean = False
    31.  
    32.     Protected Overrides Sub ValueChanged()
    33.         _ChangesMade = True
    34.     End Sub
    35.  
    36.     Public ReadOnly Property ChangesMade() As Boolean
    37.         Get
    38.             Return _ChangesMade
    39.         End Get
    40.     End Property
    41.  
    42. 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
  •  



Click Here to Expand Forum to Full Width