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
Main Topics
Browse All TopicsI 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,M
This gives similar error as follows:
"Value of Type 'namespace...meetingBase(O
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
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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
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
>>"I thought you can always cast a base class to the derived class."
You can ALWAYS cast a derived class into a base class, not the other way around
Consider the following:
Your base class is Animal, and you derived classes are Dog and Cat
Class Animal
Public Sub Eat()
End Class
Class Dog Inherits Animal
Public Sub Bark()
End Class
Class Cat Inherits Animal
Public SUb Meow()
End Class
A Dog object can eat (by virtue of being an animal), and Bark (by virtue of being a Dog)
A Cat object can eat (by virtue of being an animal), and Can Meow (by virtue of being a Cat)
A Cat cannot Bark, and a Dog cannot Meow.
Thus you can cType(Dog, Animal) after a Dog is an animal, and as such can eat.
But you cannot cType(Animal, Dog) a general Animal might not be a Dog (after all, a Cat is an Animal, but a Cat is not a Dog) So just because you have a Animal object does not necessarily mean that it is a Dog object. But if you have a Dog object, you know that it must also be an Animal object.
AW
Business Accounts
Answer for Membership
by: adam_pedleyPosted on 2007-10-27 at 22:14:46ID: 20163775
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?