Casting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Casting

    Hello,

    I have created a class that inherits MembershipUser:
    public class UserHelper : MembershipUser {
    ....

    Then I am creating a user as follows:
    UserHelper user = (UserHelper)Mem bership.CreateU ser("me", "pass",
    "me@domain.com" , null, null, true, null, out status);

    Membership.Crea teUser returns a MembershipUser

    But I get an error:
    Unable to cast object of type 'System.Web.Sec urity.Membershi pUser' to
    type 'MyApp.Security .Membership.Use rHelper'.

    If I don't cast it, which means, using:
    UserHelper user = Membership.Crea teUser("me", "pass", "me@domain.com" ,
    null, null, true, null, out status);

    It does not compile and I get the error:
    Cannot implicitly convert type 'System.Web.Sec urity.Membershi pUser' to
    'MyApp.Security .Membership.Use rHelper'. An explicit conversion exists
    (are you missing a cast?)

    What am I doing wrong?

    Thanks,
    Miguel
  • Peter Duniho

    #2
    Re: Casting

    On Sat, 01 Nov 2008 10:28:10 -0700, shapper <mdmoura@gmail. comwrote:
    Hello,
    >
    I have created a class that inherits MembershipUser:
    public class UserHelper : MembershipUser {
    ...
    >
    Then I am creating a user as follows:
    UserHelper user = (UserHelper)Mem bership.CreateU ser("me", "pass",
    "me@domain.com" , null, null, true, null, out status);
    >
    Membership.Crea teUser returns a MembershipUser
    If the object is not a UserHelper instance, you simply cannot cast it to
    UserHelper. It's just not possible.

    Note that in the second case, where you don't cast it and the compiler
    says "an explicit conversion exists", it only means that in the
    syntactical sense. It has no way to verify whether the conversion will
    actually _succeed_...jus t that you can attempt it explicitly with a cast.

    Pete

    Comment

    • Tim Roberts

      #3
      Re: Casting

      shapper <mdmoura@gmail. comwrote:
      >
      >I have created a class that inherits MembershipUser:
      >public class UserHelper : MembershipUser {
      >...
      >
      >Then I am creating a user as follows:
      >UserHelper user = (UserHelper)Mem bership.CreateU ser("me", "pass",
      >"me@domain.com ", null, null, true, null, out status);
      >
      >Membership.Cre ateUser returns a MembershipUser
      >
      >But I get an error:
      >Unable to cast object of type 'System.Web.Sec urity.Membershi pUser' to
      >type 'MyApp.Security .Membership.Use rHelper'.
      >
      >If I don't cast it, which means, using:
      >UserHelper user = Membership.Crea teUser("me", "pass", "me@domain.com" ,
      >null, null, true, null, out status);
      >
      >It does not compile and I get the error:
      >Cannot implicitly convert type 'System.Web.Sec urity.Membershi pUser' to
      >'MyApp.Securit y.Membership.Us erHelper'. An explicit conversion exists
      >(are you missing a cast?)
      >
      >What am I doing wrong?
      You're casting in the wrong direction. If you have a UserHelper, you can
      cast it to a MembershipUser, but you cannot safely start with a base object
      and cast it to a derived class. What would happen to any additional
      members you might have added? They wouldn't be present in the
      MembershipUser instance.

      In this case, you will have to use composition instead of inheritance. Add
      a MembershipUser instance to UserHelper ad a member, call CreateUser, and
      pass the MembershipUser object you get to the constructor for UserHelper.
      --
      Tim Roberts, timr@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      Working...