Link to home
Start Free TrialLog in
Avatar of sethspearman
sethspearman

asked on

Why can't I cast a base class into the derived class

I have a base class called MeetingBase that is declared like this:
Public MustInherit Class MeetingBase(Of T As MeetingBase(Of T))

I have a derived class that is declared like this:
    Public Class Meeting
         Inherits MeetingBase(Of Meeting)

In the BASE class I have this code
Protected Sub MyMethod
   'Do SomethingElse expects a Meeting object.  So why can't I do this:
   DoSomethingElse(CType(Me,T))
End Sub

It won't even compile and is showing "Value of Type 'T' cannot be converted to 'namespace...meeting'"

So I tried this:
DoSomethingElse(CType(Me,Meeting))

This gives similar error as follows:
"Value of Type 'namespace...meetingBase(Of T)' cannot be converted to 'namespace...meeting'"

I thought you can always cast a base class to the derived class.  So why this message?

The namespaces are the same.

Seth B Spearman
Avatar of adam_pedley
adam_pedley

I'm not sure on your exact problem you are mentioning but isnt the correct way meant to be

Base Class      ->      Derived Class
    Method                     Overrides Method

So that is the proper way to do polymorphism

So if you code this

Public MustInherit Class BaseClass

    Public Overridable Sub TestMethod(ByVal t As BaseClass)
        t.MethodToBeCalled()
    End Sub

    Public MustOverride Sub MethodToBeCalled()

End Class


Public Class Meeting
    Inherits BaseClass

    Public Overrides Sub TestMethod(ByVal t As BaseClass)
        MyBase.TestMethod(t)

    End Sub

    Public Overrides Sub MethodToBeCalled()
        MsgBox("hello")
    End Sub

End Class

then call it by
       Dim t As New Meeting
        t.TestMethod(t)

and you will get the msgbox "hello"

Is this what you are trying to achieve?
Hi,

>>I thought you can always cast a base class to the derived class

I thought it was the other way, you can always cast from a more general subclass to a less general (more specific) superclass
hi,

post you code for the DoSomethingElse method.

thx
Avatar of sethspearman

ASKER

Thanks for your responses.

To further explain...I do realize that this is not the norm and that you usually cast the base class into the derived class.  However in this case I know that the type consumed by my method will ALWAYS be of the type of the derived class.  (The only reason I am using base classess is so that I can code gen the base class at will.  Every one of my consumed classes will have a base for this reason).

The bottom line is that this will make the base class more helpful for my purposes

Babycorn,  The DoSomethingElse method is not relevant to the question.  I could have just as easily been doing an assignment on that line like this:   "meeting=ctype(me, T)"  as calling into a proc.  The build error is on that line and the DoSomethingElse is closer to the way my real code looks.

Thank you guys.  
Seth
One last thing...

I guess what I am saying is that I don't want to implement the MyMethod in my derived class.  I would rather only implement in the base.  But I realize (and that is my question) that may not be an option for me.  I am still learning OOP and was surprised my code was not working.  

Seth
Ok, I'm still not entirely sure on what you want but is it something like this?

Public MustInherit Class MeetingBase(Of T As MeetingBase(Of T))

    Public Sub MyMethod()

        DoSomethingElse(Me)
    End Sub

    Public MustOverride ReadOnly Property Name() As String

    Public Sub DoSomethingElse(ByVal obj As T)
        MsgBox(obj.Name)
    End Sub

End Class

Public Class Meeting
    Inherits MeetingBase(Of Meeting)

    Public Overrides ReadOnly Property Name() As String
        Get
            Return "Meeting Class"
        End Get
    End Property

End Class


ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks,

That was basically what I needed.  A yes or no answer as to whether it could be done.  I guess the answer is "NO".  And the explanation helps it to gel.

Thanks.
Glad to be of assistance

AW