New keyword - declare & instantiate at the same time

J

Jason Shohet

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
 
W

William Ryan

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
 
J

Jon Skeet [C# MVP]

William Ryan said:
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.
 
J

jason

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
 
M

mikeb

jason said:
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";
 
J

Jon Skeet [C# MVP]

jason said:
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.
 
G

Guinness Mann

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top