Casting Performance and Pattern Question

Discussion in 'ASP .Net' started by Remco, Apr 8, 2004.

  1. Remco

    Remco Guest

    Hi,

    Let me try to simply explain my questions.

    I've created a portal site with different types of users, e.g. Portal
    Administrators and Normal Users.

    One base class SessionUser (has a enum field UserType) and for each type of
    user a inherited class like SessionMasterUser and SessionNormalUser.


    Instantiating and keeping into a session:

    case Normal user:

    SessionUser user = new SessionNormalUser(loginName, pwd,
    UserType.NormalUser)

    All classes have different member variables and methods.

    At the code behind pages I check for the Enum UserType and depending on that
    I cast to the appropriate Type like ((SessionNormalUser)user).Email

    What is better to do?

    For each and everytime I need to access a member, property, or method from a
    specific class (not the base class) by Casting?
    or at places where it's needed more then once like following snippet.

    SessionNormalUser normalUser = (SessionNormalUser)user

    Question about that way: is there a performance penalty, since I understood
    that there is only a new reference created to the object.

    What's the cost of casting from a general to a more specific object?


    Pattern Related Question(s)

    In case of using the second option, which I prefer to make things easier to
    read and understand, I don't want to create a null reference to all
    different types of classes like:

    SessionNormalUser normalUser = null;
    SessionMasterUser masterUser = null;
    etc...

    I would like to create a kind of wrapper which is returning me the
    appropriate information depending on the UserType enum.
    This way should make things much more easier to maintain.

    I know a bit about the design patterns but could you please give me a some
    advice?

    Thanks in Advance,

    Remco
    Remco, Apr 8, 2004
    #1
    1. Advertising

  2. > What's the cost of casting from a general to a more specific object?
    not much expense here, casting is like putting a mask on over the face.

    >SessionNormalUser normalUser = null;

    I'm not sure why you think you need this line of code
    --
    Regards,
    Alvin Bruney [ASP.NET MVP]
    Got tidbits? Get it here...
    http://tinyurl.com/27cok
    "Remco" <> wrote in message
    news:...
    > Hi,
    >
    > Let me try to simply explain my questions.
    >
    > I've created a portal site with different types of users, e.g. Portal
    > Administrators and Normal Users.
    >
    > One base class SessionUser (has a enum field UserType) and for each type
    > of
    > user a inherited class like SessionMasterUser and SessionNormalUser.
    >
    >
    > Instantiating and keeping into a session:
    >
    > case Normal user:
    >
    > SessionUser user = new SessionNormalUser(loginName, pwd,
    > UserType.NormalUser)
    >
    > All classes have different member variables and methods.
    >
    > At the code behind pages I check for the Enum UserType and depending on
    > that
    > I cast to the appropriate Type like ((SessionNormalUser)user).Email
    >
    > What is better to do?
    >
    > For each and everytime I need to access a member, property, or method from
    > a
    > specific class (not the base class) by Casting?
    > or at places where it's needed more then once like following snippet.
    >
    > SessionNormalUser normalUser = (SessionNormalUser)user
    >
    > Question about that way: is there a performance penalty, since I
    > understood
    > that there is only a new reference created to the object.
    >
    > What's the cost of casting from a general to a more specific object?
    >
    >
    > Pattern Related Question(s)
    >
    > In case of using the second option, which I prefer to make things easier
    > to
    > read and understand, I don't want to create a null reference to all
    > different types of classes like:
    >
    > SessionNormalUser normalUser = null;
    > SessionMasterUser masterUser = null;
    > etc...
    >
    > I would like to create a kind of wrapper which is returning me the
    > appropriate information depending on the UserType enum.
    > This way should make things much more easier to maintain.
    >
    > I know a bit about the design patterns but could you please give me a some
    > advice?
    >
    > Thanks in Advance,
    >
    > Remco
    >
    >
    >
    Alvin Bruney [MVP], Apr 11, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,798
    Andrew McDonagh
    Jan 8, 2005
  2. sunny
    Replies:
    1
    Views:
    461
    Salt_Peter
    Dec 7, 2006
  3. Wally Barnes
    Replies:
    3
    Views:
    519
    Wally Barnes
    Nov 20, 2008
  4. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    559
    James Kanze
    Dec 20, 2009
  5. Replies:
    6
    Views:
    214
    Arved Sandstrom
    Dec 26, 2013
Loading...

Share This Page