473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1908
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
6
by: RepStat | last post by:
I've read that it is best not to use exceptions willy-nilly for stupid purposes as they can be a major performance hit if they are thrown. But is it a performance hit to use a try..catch..finally...
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
1
by: Remco | last post by:
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...
2
by: Nacho | last post by:
Hi, I'm designing a new database and I have a doubt in which surely you can help me. I'm storing in this database historical data of some measurements and the system in constantly growing, new...
6
by: Ken Fine | last post by:
This is a basic question. What is the difference between casting and using the Convert.ToXXX methods, from the standpoint of the compiler, in terms of performance, and in other ways? e.g. ...
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.