473,722 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Specified cast is invalid error using my own class?

I'm a newbie to vb.net, and classes were something I never played with in
"standard" vb, but...

Basically,part of my program comprises of some tcp/ip stuff, and I used the
following code to start off the connection:

Public Sub Main()
Const PortNumber As Integer = 8022
Dim tcpListener As New TcpListener(Por tNumber)
tcpListener.Sta rt()
Try
'Wait for a connection.
Dim theClient As SubTCPClient = tcpListener.Acc eptTcpClient()
Dim networkStream As NetworkStream = theClient.GetSt ream()

Now, the "subtcpclie nt" used to be just tcpclient (i.e. the built-in base
class) and all worked well. I needed access to some of the "protected"
properties and methods though, so after some googling I found a needed to
create my own sub class. This is done as:

Public Class SubTCPClient
Inherits TcpClient
Public Sub New()
End Sub
Public Function IsConnActive() As Boolean
IsConnActive = Me.Active
End Function
End Class

But since I did this it no longer works. There aren't any errors in the dev
environment, and the program runs, but as soon as a client connection is
received (the "dim client as subtcpclient... blah line), I get the
"specified cast is invalid" error.
It looks like this is to do with type conversion, but I can't see where I am
doing that, and my new class is based on the same one as it used to work
with, so I don't think thats it...

Any ideas?
James
Nov 21 '05 #1
2 1621
Your supplied will run without error (despite the warning about your
obsolete usage of the New TcpListener call), just hanging on the
AcceptTcpClient line waiting for the client. So the actual error
obviously has something to do with when/how the connection is made. In
short, you haven't given enough detail for anyone to help ya decipher
the problem here.

Nov 21 '05 #2
James wrote:
tcpListener.Acc eptTcpClient returns a TCPClient object not a
SubTCPClient object. Even though your SubTCPClient class inherits from
TCPClient.

Firstly, do you have Option Strict On at the top of your code?

Secondly, what happens if you cast the return value from the
AcceptTcpClient method like this: (I divided the lines so my reply
would fit better):

Dim theClient As SubTCPClient

theClient = DirectCast(tcpL istener.AcceptT cpClient(),SubT CPClient)

Chris

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3635
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception: "Specified cast is not valid." The only thing i put there was a "test this." inside the form. What might be the problem here? Thanks in advance. The Exception:
6
4735
by: Mike Spass | last post by:
Hello, i have a problem to cast an object - hope somebody can help me. thx namespace CopyTest { public class A { public int a;
0
1176
by: Andrew | last post by:
Can someone out there help. I am using Server. Transfer from a user control to a webform and tried to follow many examples exactly as specified but cannot get around the (Specified cast is not valid.) ERROR which happens on the second page Search.aspx on the line with (PrevPageValues = (FirstPage)Context.Handler;). I have also got <%@ Reference Page="_FirstPage.ascx" %> at the top of the second page.
5
3431
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
3
10514
by: PK9 | last post by:
I am looking for assistance in pinpointing the cause of the following exception. I am getting a "Specified Cast is not valid" exception on my page. I am trying to populate a datagrid. One of my columns is a template column where I'd like to evaluate the data brought back from the db and populate the column with a "Y" or "N" depending on the value in the db. Here is the code that is causing it as well as the error (below). ERROR...
6
2318
by: John-Arne Lillebø | last post by:
Hi. I run into this problem and i could need some help to solve it. The project is an ASP.NET Web project. Including code sample of the problem. Any idea what is causing the error message ? John-Arne Lillebø
0
1383
by: df | last post by:
I'm seeing a strange problem in the Web App config tool: After creating a role and a user, going back to the Security tab home page, I get a "Specified cast is not valid" error. I've set up my web application to use SQL Server to store the Membership database using aspnet_regsql.exe and have successfully opened the Web Config Tool. Then, I create a role, and go look in the SQL Server aspnet_roles table, and there is the role. All...
2
3531
by: Kashiefah | last post by:
Hi, I keep on receiving this error when I click on the edit email link from the datagrid:Specified cast is not valid. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Specified cast is not valid. Source Error: Line 44: while...
3
12434
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ====================== Message: Specified cast is not valid. Type: System.InvalidCastException Source: System.Data.Linq TargetSite: Boolean TryCreateKeyFromValues(System.Object, V ByRef)
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.