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