By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

InvalidCastException

P: n/a
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!
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

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!

Nov 21 '05 #2

P: n/a
Thanks. I'll create a new constructor that takes a TcpClient as a parameter.
Nov 21 '05 #3

P: n/a
i don't used ctype . but i used this...
myclient = New TcpClient(ipbox.Text, portbox.Text)
regards,

some guy wrote:
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!


Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.