InvalidCastException

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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!
 
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
 
i don't used ctype . but i used this...
myclient = New TcpClient(ipbox.Text, portbox.Text)
regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top