August 17th, 2003, 07:31 PM
VB.NET CType function in C#? all i get is specified cast not valid
I have some trouble converting this vb.net code to C#. Mainly the problem is to find a replacement for the vb.net CType function
I have this code in vb.net
This code is in a user control and FormBase is a class that inherits from System.Web.UI.Page
Dim myUser As User
myUser = New User(userId)
Dim objPage As FormBase
objPage = CType(Me.Page, FormBase) <---- I don't know how to do this in C#
objPage.CurrentUser = myUser
I've tried this:
and all i get is "specified cast is not valid
objPage = (FormBase) this.Page;
complete error message:
Thanks for your help..
Exception Details: System.InvalidCastException: Specified cast is not valid.
Line 68: myUser.Validate(txtUsername.Text.Trim(), txtPassword.Text.Trim());
Line 69: FormBase objPage;
Line 70: objPage =(FormBase)this.Page;
Line 71: objPage.CurrentUser = myUser;
Source File: c:\inetpub\wwwroot\temp\userlogin.ascx.cs Line: 70
August 17th, 2003, 10:30 PM
Nevermind. Problem solved
December 7th, 2004, 10:17 PM
this is a link to a quick vb.net to c# converter. It's not 100% but it can help you get where you want to go.
I did a cut and past with your code above and it spit back:
myUser = new User(userId);
objPage = ((FormBase)this.Page);
objPage.CurrentUser = myUser;
December 8th, 2004, 06:56 AM
There is also a great, pocket reference book called "C# & VB.Net Conversion" by O'Reilly books.
I own this book, and it is perfect for converting to/from C#/VB.Net.
December 9th, 2004, 01:44 PM
For everyone that might run into the same problem, in order to cast in C# all you have to do is this:
The above code will cast the myRow[""] Object as an int. This will throw an exception if the cast is done on a string, etc.
int i = (int)myRow["OrderNbr"];