473,383 Members | 1,813 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,383 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 1232

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...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.