#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Portugal
    Posts
    106
    Rep Power
    12

    Unhappy 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

    Code:
             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:
    Code:
    FormBase objPage;
    
    objPage = (FormBase) this.Page;
    and all i get is "specified cast is not valid

    complete error message:
    Code:
    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..
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Portugal
    Posts
    106
    Rep Power
    12
    Nevermind. Problem solved
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    TX
    Posts
    7
    Rep Power
    0
    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:

    Code:
    User myUser; 
    myUser = new User(userId); 
    FormBase objPage; 
    objPage = ((FormBase)this.Page); 
    objPage.CurrentUser = myUser;
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    12
    Rep Power
    0

    great book


    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.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    12
    Rep Power
    0

    Real quick...


    For everyone that might run into the same problem, in order to cast in C# all you have to do is this:

    Code:
           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.

IMN logo majestic logo threadwatch logo seochat tools logo