persistant variables with local scope?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Roper

    #1

    persistant variables with local scope?

    Is there a C# approach that allows you to define locals with program
    lifetime? That is, I don't want to have to expand the scope of a variable
    just so I can make it persistent.

    For example, something like:

    class myClass
    {
    int iCallCounter = 0;
    int CallCounter() { return ++iCallCounter }
  • Michael Mayer

    #2
    Re: persistant variables with local scope?


    "Michael Roper" <michaelr@encra ft.com> wrote in message
    news:bir2g6$cbs cv$1@ID-160215.news.uni-berlin.de...[color=blue]
    > Is there a C# approach that allows you to define locals with program
    > lifetime? That is, I don't want to have to expand the scope of a variable
    > just so I can make it persistent.[/color]
    No other approach, you have to make it a class variable.
    [color=blue]
    > Is there an elegant way to do this in the C# world?[/color]

    The most elegant is to use a naming convention that helps define that a var
    is only to be used by that method. I start the var with the same name as
    the method, maybe something like below. This helps to keep a variable
    assoicated with a method it "belongs" to.

    class myClass
    {
    int callCounterInde x = 0;
    int CallCounter() { return ++iCallCounter }

    bool anotherMethodHa sBeenRun = false;
    void anotherMethod ()
    {
    if (!anotherMethod HasBeenRun)
    { blah; }
    anotherMethodHa sBeenRun = true;
    }
    }

    --
    Mike Mayer

    mike@mag37.com


    Comment

    • Michael Mayer

      #3
      Re: persistent variables with local scope?

      "Michael Roper" <michaelr@encra ft.com> wrote in message
      news:birdpn$cgf mb$1@ID-160215.news.uni-berlin.de...[color=blue]
      > Michael Mayer writes:[color=green]
      > > No other approach, you have to make it a class variable.[/color]
      >
      > Thanks Michael. Is there a good reason this was left out of the language?
      > Seems odd not to make scope and lifetime independent attributes where it
      > makes sense to do so. Is it that information hiding is not considered as
      > important as lifetime, or am I missing the big picture?
      >[/color]

      I agree with you that it seems odd to be missing (but that's just my C++
      heritage talking). I'm sure a lot of people from the Java camp would make
      comments about the language being simpler this way. Classes hold state, not
      methods, etc. If you do a google search within this newsgroup for "static
      local variable" you'll find many threads on this topic. I suppose a very
      similar case is variables that are tied to just a property, but not
      class-wide.

      The language has many mechanisms to help you encapsulate date (hide it) from
      the "outside" world. There are very few mechanisms for futher encapsulating
      data within a class from other parts of the class. If your classes are
      small enough and simple enough, you shouldn't have too many problems just
      letting your variables be class-wide. When your classes start to get big
      and are developed by many developers, that's when it would be nice to have
      some ways to hide data better. I suppose the only thing we can do now is to
      try to keep classes as simple as possible.

      --
      Mike Mayer

      mike@mag37.com



      Comment

      • Jon Skeet

        #4
        Re: persistent variables with local scope?

        Michael Mayer <mike@mag37.com > wrote:[color=blue]
        > I agree with you that it seems odd to be missing (but that's just my C++
        > heritage talking). I'm sure a lot of people from the Java camp would make
        > comments about the language being simpler this way. Classes hold state, not
        > methods, etc.[/color]

        The latter is precisely the reason, which happens to lead to the
        former.

        You're essentially wanting state associated with an instance or with
        the type itself, so declare it that way.

        --
        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

        Working...