Casting works when the object being cast "knows about" the object it is
being cast to. In your example the custom class Client "knows about"
TcpClient. This means that Client can be cast to TcpClient. However,
TcpClient does not "know about" Client. TcpClient's definition has nothing
to do with Client so it does not even know Client exist. Therefore you can
not cast TcpClient to Client.
Now I am guessing your next question would be, "So how do I get a Client
from a TcpClient?". There are a few ways of doing this. One way would be
to create a constructor for Client that takes a TcpClient and performs the
necessary operations to fill the base TcpClient class of Client with the
prosperities of the passed TcpClient.
Robby
"some guy" <so*****@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
Hi,
I've got a class called Client that inherits from TcpClient. I've also got
a
TcpListener set up, which then tries to convert the TcpClient received
from
AcceptTcpClient to my Client class, eg:
Dim NewClient as Client = CType(Server.AcceptTcpClient, Client)
I've also tried with DirectCast, but neither work and they both throw
InvalidCastExceptions.
Thanks in advance for your help!