How to cast to a Class that has a Super

Lets say I have a plugin that has a class called Car.

I have my own class called Tesla that has a Super of Car with all sorts of extra functionality.

The function Tesla.Start returns a reference to a new Car but I want to assign it to a Tesla.

I guess it would be a cast of some kind, but I cant find any docs on it.

I’ve tried

Dim oTesla As Tesla = Tesla.Start()

but I get a Type mismatch error. Expected class Tesla, but got class Car.

Thanks in advance.

You use the class name as a function to perform a Cast.

Dim oTesla As Tesla = Tesla(Tesla.Start())

Tim, I could hug you! Thanks!

Why does Tesla.Start not return a Tesla?

Because Tesla has is (corrected) a Super of Car, and Car.Start returns Car, so Tesla.Start also returns Car

Car is the SuperClass of Tesla.

Tesla is a Subclass of Car.

I still feel there is a better way …

I’m open to suggestions :slight_smile:

I agree with Markus. Is there a Start function in your Tesla subclass, or does it simply inherit the Start function from Car? If you don’t have a Start function that actually returns a Tesla object, no amount of casting is going to solve your problem.

Make Tesla.Start return a Tesla - not a Car

ie/

Class Car
    Shared Sub Start() as Car
    Sub
End Class

Class Tesla
  Inherits Car
    Shared Sub Start() as Tesla
    Sub
End Class

And this makes more sense since the Tesla class knows how ot create and return a Tesla ( a more specific type of car) not a generic Car

Thanks for the help all.

Hmm, drat, I didnt test the whole execution before thanking you Tim, serves me right for staying up into the wee hours. It compiles and runs. but I get IllegalCastException (Car cannot be cast to Tesla) when its called.

Dim oTesla As Tesla = Tesla(Car.Start()) <-- IllegalCastException (Car cannot be cast to Tesla)

I dont have access to Car, its a class from a plugin.

[code]Class Car
Shared Sub Start() as Car
Sub
End Class

Class Tesla
Inherits Car
Shared Sub Start() as Tesla
return Tesla(Super.Start()) <-- IllegalCastException (Car cannot be cast to Tesla)
Sub
End Class[/code]

Basically, I need to create a Class that I can pass the Car returned by Car.Start

This class would then let me use the events etc raised by Car.

I was trying to keep it abstract to keep it simple but everyone is assuming that I have control over Car and the return values from Car.Start which I dont. Maybe this would be easier without all the abstraction…

Car is Chilkat.Socket
Start is AcceptNextConnection
Chilkat.Socket.AcceptNextConnection returns a new Chilkat.Socket

aka

Car.Start returns a new Car

I created a Class as follows

Class TCPConnection
Inherits Chilkat.Socket
End Class

I want to somehow assign the Chilkat.Socket returned from AcceptNextConnection to my TCPConnection class so I can then create the functionality in that class to deal with the socket, events etc.

I could drop the Super in TCPConnection and just connect things up manually with AddHandler inside TCPConnection but I was wondering if there was a way without doing this.

[quote=312733:@JulianS]Thanks for the help all.

Hmm, drat, I didnt test the whole execution before thanking you Tim, serves me right for staying up into the wee hours. It compiles and runs. but I get IllegalCastException (Car cannot be cast to Tesla) when its called.

Dim oTesla As Tesla = Tesla(Car.Start()) ← IllegalCastException (Car cannot be cast to Tesla)
[/quote]
Right a Tesla ISA Car and not the other way round
You can cast from SUBCLASS to SUPER class but not from Super to Sub

Wrapping it (composition) rather than subclassing may be the only way to do this

BUT I dont have the plugin to play with (nor the time)

Create a Wrapper class that handles all the events of the socket and just re-raises its own events. Same with any methods, just call through to the captive socket that the class wraps.

Righty-ho guys a wrapper it is! Thanks! I’ll have a play around with the plugin to make sure it can do what I want before I get stuck into that. At least I know what direction I need to go now to get that sorted, cheers.