Helpful Information
Home
Categories
Contact us
Category: .Net Development
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
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
This code is in a user control and FormBase is a class that inherits from System.Web.UI.Page
I've tried this:
FormBase objPage;
objPage = (FormBase) this.Page;
and all i get is "specified cast is not valid
complete error message:
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
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
Thanks for your help..
Nevermind. Problem solved :)
http://developerfusion.com/utilities/convertvbtocsharp.aspx
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:
User myUser;
myUser = new User(userId);
FormBase objPage;
objPage = ((FormBase)this.Page);
objPage.CurrentUser = myUser;
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.
For everyone that might run into the same problem, in order to cast in C# all you have to do is this:
int i = (int)myRow["OrderNbr"];
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.
privacy (GDPR)