I needed a class that would have everything TcpClient has + a couple extra fields so I created a class based on TcpClient:
public class MyClient: TcpClient
{
public string winUser;
public bool needHelp;
public bool needSignOff;
public MyClient() : base() {
this.needHelp = false;
this.needSignOff = false;
this.winUser = "empty";
}
}
Now I need all incoming connections to be created as objects of this class:
MyClient clientConnection = (MyClient)this.helpServer.AcceptTcpClient();
The problem is that AcceptTcpClient() generates a TcpClient class object and I keep getting this exception when I try to connect to server:
listenServer(): Unable to cast object of type ‘System.Net.Sockets.TcpClient’ to type ‘Server.MyClient’.
How do I convert a TcpClient object that AcceptTcpClient() gives me into MyClient object?
It’s not possible to downcast a type (
TcpClient) to a more derived type (MyClient). You have to updateAcceptTcpClientto return aMyClientobject.