473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

InvalidCastException

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
3 1229

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
Thanks. I'll create a new constructor that takes a TcpClient as a parameter.
Nov 21 '05 #3
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Johan Appelgren | last post by:
Hi, I'm trying to serialize a class hierarchy using System.Xml.Serialization.XmlSerializer from a class that is loaded as a Com object. But it fails with a InvalidCastException. I don't know...
1
by: bob scola | last post by:
I have a csharp, VS 2003 solution for a winform application The application uses an object called a "matter" and the class is defined in matter.cs. I can load matter objects into a combobox ...
11
by: Roy Lawson | last post by:
I have no idea what is going on here. I wrote a simple application in VB.NET to generate a Crystal Report, and I am now trying to move it to ASP.NET with Crstal Enterprise. I wish I could tell...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
8
by: =?Utf-8?B?YXVsZGg=?= | last post by:
i trying to collect windows services. i'm getting inconsistencies. the collection works every time locally. but remote collection yields different result depending on my lab environment. 1) in...
4
by: rsdev | last post by:
Hi, I have an InvalidCastException which is completely puzzling me. I have checked all the members in the stored procedure against my data provider and seems to be ok. Also in the stack trace it...
8
by: Joe HM | last post by:
Hello - I was wondering that the "cleanest" way is to determine whether a CType() will throw an InvalidCastException? I have data I receive as an Object and I want to convert it to a String...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.