New keyword - declare & instantiate at the same time

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Shohet

    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


  • William Ryan

    #2
    Re: New keyword - declare & instantiate at the same time

    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" <ash477@hotmail .com> wrote in message
    news:usUo53okDH A.392@TK2MSFTNG P11.phx.gbl...[color=blue]
    > 2 questions for anyone who can answer:
    >
    > 1. in class declarations, I realize you have to use the NEW keyword if[/color]
    you[color=blue]
    > 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[/color]
    a[color=blue]
    > 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[/color]
    in[color=blue]
    > every function that I need objUser?:
    > public cUser objUser = (cUser)Session["objUser"];
    >
    > Thanks for the clarification
    > Jason Shohet
    >
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: New keyword - declare &amp; instantiate at the same time

      William Ryan <dotnetguru@nos pam.comcast.net > wrote:[color=blue]
      > 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?[/color]

      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 - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • jason

        #4
        Re: New keyword - declare &amp; instantiate at the same time

        > big object. Now, if the user does tasks A, B, and C, all of the functions[color=blue]
        > will need to see myObject.[/color]

        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.

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

        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.usernam e = "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

        Comment

        • mikeb

          #5
          Re: New keyword - declare &amp; instantiate at the same time

          jason wrote:
          [color=blue][color=green]
          >>big object. Now, if the user does tasks A, B, and C, all of the functions
          >>will need to see myObject.[/color]
          >
          >
          > 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.
          >
          >
          >[color=green]
          >>If (cUser)Session["objUser"]; already exists, you can reference it directly
          >>without declaring objUser referenced on the left hand side.[/color]
          >
          >
          > 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.usernam e = "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[/color]

          try

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

          --
          mikeb

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: New keyword - declare &amp; instantiate at the same time

            jason <ash477@hotmail .com> wrote:
            [color=blue]
            > I can't seem to do
            > (ShohetServices .cUser)Session["objUser"].username = "xyz"
            > as you were perhaps implying above?[/color]

            You should be able to if you put appropriate brackets in:

            ((ShohetService s.cUser)Session["objUser"]).username = "xyz";
            [color=blue]
            > I'm always one for saving a line or two of code :)[/color]

            I'm not - it often kills readability.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Guinness Mann

              #7
              Re: New keyword - declare &amp; instantiate at the same time

              In article <MPG.19f796c042 ea0f3098989f@ms news.microsoft. com>,
              skeet@pobox.com says...[color=blue]
              > You should be able to if you put appropriate brackets in:
              >
              > ((ShohetService s.cUser)Session["objUser"]).username = "xyz";
              >[color=green]
              > > I'm always one for saving a line or two of code :)[/color]
              >
              > I'm not - it often kills readability.[/color]

              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

              Comment

              Working...