473,385 Members | 1,757 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,385 software developers and data experts.

New keyword - declare & instantiate at the same time

2 questions for anyone who can answer:

1. in class declarations, I realize you have to use the NEW keyword if you
want to declare & instantiate some class at the same time. Whats an
advantage for just declaring at the top of say, an aspx page, and then
instantiating when you need it in various functions in the page?

2. I can't seem to figure out a way to use the NEW keyword to instantiate a
class coming from a session object. ie, where do I put the NEW keyword in
below, so that I can grab from the sesion only once in the page, and not in
every function that I need objUser?:
public cUser objUser = (cUser)Session["objUser"];

Thanks for the clarification
Jason Shohet
Nov 15 '05 #1
6 2437
Declaring without instantiating allows you to scope your variable in the
chance that you'll need it, but not waste resources if you don't need it.
For instance, I might have a form with multiple controls referencing some
big object. Now, if the user does tasks A, B, and C, all of the functions
will need to see myObject. However, if they don't select A, B, or C, and
instead, only do D and F, then I never need myObject so why waste resources
instantiating it?

If (cUser)Session["objUser"]; already exists, you can reference it directly
without declaring objUser referenced on the left hand side. Since you can
reference the object directly, declaring another object of the same type,
that may have smaller scope may not be well advised. Also remember that
after a post back, unless you reset variables, the pages state and variable
state will be the same as it was the first time the page was loaded. If you
need to reaccess the data in the Session object, I'd recommend referencing
it directly. You lose a little readability in some regards, but your
intentions are very clear and it's a little more straightforward.

HTH,

Bill
" Jason Shohet" <as****@hotmail.com> wrote in message
news:us*************@TK2MSFTNGP11.phx.gbl...
2 questions for anyone who can answer:

1. in class declarations, I realize you have to use the NEW keyword if you want to declare & instantiate some class at the same time. Whats an
advantage for just declaring at the top of say, an aspx page, and then
instantiating when you need it in various functions in the page?

2. I can't seem to figure out a way to use the NEW keyword to instantiate a class coming from a session object. ie, where do I put the NEW keyword in
below, so that I can grab from the sesion only once in the page, and not in every function that I need objUser?:
public cUser objUser = (cUser)Session["objUser"];

Thanks for the clarification
Jason Shohet

Nov 15 '05 #2
William Ryan <do********@nospam.comcast.net> wrote:
Declaring without instantiating allows you to scope your variable in the
chance that you'll need it, but not waste resources if you don't need it.
For instance, I might have a form with multiple controls referencing some
big object. Now, if the user does tasks A, B, and C, all of the functions
will need to see myObject. However, if they don't select A, B, or C, and
instead, only do D and F, then I never need myObject so why waste resources
instantiating it?


On the other hand, if you're not going to use the variable, why pollute
your variable namespace by declaring it in the first place?

I only rarely declare variables without immediately assigning to them,
and I certainly keep the point of declaration as close to the point of
first use as possible.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
> big object. Now, if the user does tasks A, B, and C, all of the functions
will need to see myObject.
This is my situation. I'm not worried about resources, i'd rather
have it declared above. And make my code simpler, more readable in
the functions.

If (cUser)Session["objUser"]; already exists, you can reference it directly
without declaring objUser referenced on the left hand side.


How do I do this? I tried, but it takes me 2 lines to do use objUser,
ie:

1. ShohetServices.cUser objUser =
(ShohetServices.cUser)Session["objUser"]
2. objUser.username = "xyz"

I can't seem to do
(ShohetServices.cUser)Session["objUser"].username = "xyz"
as you were perhaps implying above? I'm always one for saving a line
or two of code :)

Rgds,
Jason Shohet
Nov 15 '05 #4
jason wrote:
big object. Now, if the user does tasks A, B, and C, all of the functions
will need to see myObject.

This is my situation. I'm not worried about resources, i'd rather
have it declared above. And make my code simpler, more readable in
the functions.
If (cUser)Session["objUser"]; already exists, you can reference it directly
without declaring objUser referenced on the left hand side.

How do I do this? I tried, but it takes me 2 lines to do use objUser,
ie:

1. ShohetServices.cUser objUser =
(ShohetServices.cUser)Session["objUser"]
2. objUser.username = "xyz"

I can't seem to do
(ShohetServices.cUser)Session["objUser"].username = "xyz"
as you were perhaps implying above? I'm always one for saving a line
or two of code :)

Rgds,
Jason Shohet


try

((ShohetServices.cUser) Session["objUser"]).username = "xyz";

--
mikeb

Nov 15 '05 #5
jason <as****@hotmail.com> wrote:
I can't seem to do
(ShohetServices.cUser)Session["objUser"].username = "xyz"
as you were perhaps implying above?
You should be able to if you put appropriate brackets in:

((ShohetServices.cUser)Session["objUser"]).username = "xyz";
I'm always one for saving a line or two of code :)


I'm not - it often kills readability.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
In article <MP************************@msnews.microsoft.com >,
sk***@pobox.com says...
You should be able to if you put appropriate brackets in:

((ShohetServices.cUser)Session["objUser"]).username = "xyz";
I'm always one for saving a line or two of code :)


I'm not - it often kills readability.


And makes it *darned* hard to debug. Sometimes after my code is working
I'll go back and make simple optimizations like that, but not usually.

(A case where I *might* do it? When I'm delivering source code to a
customer and I want to make darned sure that if he ever wants it
modified he's got to come to me. :-) )

-- Rick
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jeff Magouirk | last post by:
Dear Group, I am trying to create a view and keep getting the Incorrect syntax near the keyword 'Declare'" error. Here is the code I am writing. Create view fixed_airs (sid, fad_a2, fad_a3)...
0
by: Dale Ring | last post by:
Access 2000 Each record has several time fields (short time format). I'm using the following 2 Textboxes to determine actual "Scene Time" =DateDiff("n",,) =\60 & Format( Mod 60,"\:00") ...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
2
by: N. Demos | last post by:
I have a user control with code behind of which two instances are created/declared in my aspx page. The aspx page has code behind also, as I need to access methods of the usercontrols on page...
7
by: dc | last post by:
Can anybody think of a situation where virtual function's address resolution/ something related to virtual is done at compile time
4
by: thinktwice | last post by:
i have just made a test project :(win32 console) //file : func.h #ifndef _FUNC_H_ #define _FUNC_H_ void func1() { return; };
0
by: typingcat | last post by:
I live in GMT+9 The SQL server is in GMT+0 I used GridView to display 'time' column from SQL server. I need to add 9 hours for each time but how can I do this? Can I solve this by query statement...
0
by: vraj2k3 | last post by:
Hi Friends, I want to know how to declare an array as GLOBAL and how to call that array. Reply soon...
0
by: onlymukti4u | last post by:
Hi All, Can anyone help me out to get the code for knowing the login and logout time difference between some pages. What exactly I want is...."I have a small project having 5 pages,once the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.