Question cast object from base class to derived class

zatalian

Member
Joined
Sep 30, 2007
Messages
10
Programming Experience
10+
Hi,

I'm trying to cast an object from its baseclass to my inherited class. I think it's called downcasting and from what i googled, this seems impossible in vb.net??

This is what i want to do :

I'm writing a tcp client / server application

for the client, i use TcpClient, but i inherited this class to K8055Client. An object of this class automatically start reading and creates events when commands arrive from the server...

I use this class K8055Client on both sides.

At the server side, at some point, i get an EndAcceptTcpClient function which returns me a TcpClient. How can i convert this TcpClient to K8055Client??

Again, at the server side, i want to place all my clients in a listbox. So i inherited K8055Client and made a class K8055ListViewItem.
So, at some point in my serverprogram where i want to add a client to my listbox, i get an object of type K8055Client that i want to convert to K8055ListViewItem

How would you program this situation, if you can't downcast?

I know i can add tcpclient as a member in K8055client
and add K8055Client as a member in K8055ClientListView but that does not solve my problem :

At some point i get a client who wants to disconnect : Type K8055Client. At that point i would like to do : ListBox.Items.Remove(client). This is only possible if K8055Client is a listviewitem and a tcpclient at the same time. This does not work when the K8055 client is a member of the class K8055ListViewItem...

I hope that what i wrote does make sense to anybody? How would i solve a situation like this?
 
What is this "K8055Client" class you are referring to? It is not in .Net class library and not on Google/Internet.
In general you can't cast a base class instance to a derived class, if the derived class can be constructed from a base instance it should have a constructor that take this as parameter, like Sub New(client as tcpclient).
 
K8055Client is a derived class i'm trying to write and should have TcpClient as parent.

At some point I would like to use the tcpClient i get from EndAcceptTcpClient as a K8055Client, with all the extra functions I wrote for it.

Is it possible to write a constructor
VB.NET:
Class K8055Client
    Inherits TcpClient

Public sub New(tcpClient As TcpClient)
   Me = tcpClient
End Sub

End Class
Is that what you are suggesting? I'll try it out...
 
No, as known you can't cast a TcpClient instance to a derived class. "aK8055 = aTcpClient" is implicitly the same as "aK8055 = CType(aTcpClient, K8055Client)". It would mean less errors and erroneous assumptions in code if you turned on Option Strict.

It is also not possible for a class to assign a new instance to itself (Me).
'Me' cannot be the target of an assignment.
The point of constructing an instance from parameters is to use the provided values to initialize it, this means that key properties of the class is set from the given values. Since TcpClient is a wrapper for a Socket, for which you have an established connection you want to use, you can take it from the Client property:
Gets or sets the underlying Socket.
VB.NET:
Public Sub New(tcp As TcpClient)
   Me.Client = tcp.Client
End Sub
 
Back
Top