472,114 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,114 software developers and data experts.

Casting Performance and Pattern Question

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

Nov 18 '05 #1
2 1809
> 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" <re*****@hotmail.com> wrote in message
news:eA**************@TK2MSFTNGP11.phx.gbl... 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

Nov 18 '05 #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" <re*****@hotmail.com> wrote in message
news:eA**************@TK2MSFTNGP11.phx.gbl... 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

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jacob Jensen | last post: by
3 posts views Thread by Kurt | last post: by
reply views Thread by Kurt Lange | last post: by
6 posts views Thread by RepStat | last post: by
18 posts views Thread by Marco | last post: by
1 post views Thread by Remco | last post: by
19 posts views Thread by =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.