is there 'static' variable inside a method?

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

    #1

    is there 'static' variable inside a method?

    in c++ if i put
    static int count = 0;
    in a method; count can only accessed inside the method and retain it's
    value.

    is there equilvant thing in C#?
    thx


  • dominic paquette

    #2
    is there 'static' variable inside a method?


    Yes there is a static modifier in c#. However you can't
    declare a static variable inside a function.
    [color=blue]
    >-----Original Message-----
    >in c++ if i put
    >static int count = 0;
    >in a method; count can only accessed inside the method[/color]
    and retain it's[color=blue]
    >value.
    >
    >is there equilvant thing in C#?
    >thx
    >
    >
    >.
    >[/color]

    Comment

    • Michael Culley

      #3
      Re: is there 'static' variable inside a method?

      vb.net allows this but not C# for some reason.

      "Action" <actionatadista nce@hotmail.com > wrote in message
      news:uKkehfAfDH A.2484@TK2MSFTN GP09.phx.gbl...[color=blue]
      > in c++ if i put
      > static int count = 0;
      > in a method; count can only accessed inside the method and retain it's
      > value.
      >
      > is there equilvant thing in C#?
      > thx
      >
      >[/color]


      Comment

      • William Stacey

        #4
        Re: is there 'static' variable inside a method?

        I wonder why vb allows this? Hold over from vb6? Are statics in a method
        accessable from the class itself or just inside the method?

        --
        William Stacey, DNS MVP

        "Michael Culley" <mike@nospam.co m> wrote in message
        news:OrulwsAfDH A.2332@TK2MSFTN GP12.phx.gbl...[color=blue]
        > vb.net allows this but not C# for some reason.
        >
        > "Action" <actionatadista nce@hotmail.com > wrote in message
        > news:uKkehfAfDH A.2484@TK2MSFTN GP09.phx.gbl...[color=green]
        > > in c++ if i put
        > > static int count = 0;
        > > in a method; count can only accessed inside the method and retain it's
        > > value.
        > >
        > > is there equilvant thing in C#?
        > > thx
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Michael Culley

          #5
          Re: is there 'static' variable inside a method?

          "William Stacey" <staceyw@mvps.o rg> wrote in message
          news:%23DJ6i3Af DHA.1212@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          > I wonder why vb allows this? Hold over from vb6?[/color]

          Yep, it's due to compatibility with vb6.
          [color=blue]
          > Are statics in a method
          > accessable from the class itself or just inside the method?[/color]

          Just inside the method. If you want a variable to be static for the entire
          class you need to declare it as 'Shared'.

          Regards,
          Michael Culley


          Comment

          • Jeff Louie

            #6
            Re: is there 'static' variable inside a method?

            Um Er. Static has at least four different meanings in c++.

            void call() {
            static int num=0;
            num++;
            }

            num has local scope and application lifetime. num is initialized on the
            first call() and retains its value between calls. yup, between calls.

            Regards,
            Jeff[color=blue]
            >Yes there is a static modifier in c#. However you can't[/color]
            declare a static variable inside a function.<



            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Action

              #7
              Re: is there 'static' variable inside a method?

              o...too bad
              as it seems that i will make the code cleaner by making a variable that
              will only be used in a method static.



              "dominic paquette" <dom_paq@yahoo. fr> wrote in message
              news:07c901c37c 0a$8c2f8eb0$a40 1280a@phx.gbl.. .[color=blue]
              >
              > Yes there is a static modifier in c#. However you can't
              > declare a static variable inside a function.
              >[color=green]
              > >-----Original Message-----
              > >in c++ if i put
              > >static int count = 0;
              > >in a method; count can only accessed inside the method[/color]
              > and retain it's[color=green]
              > >value.
              > >
              > >is there equilvant thing in C#?
              > >thx
              > >
              > >
              > >.
              > >[/color][/color]


              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: is there 'static' variable inside a method?

                William,
                Its a holdover from VB6, if you look at the IL, VB.NET creates one or two
                class fields to hold the function level static.

                One class field is the static variable itself. The second is a field used to
                see if the static variable was initialized or not.
                [color=blue]
                > Are statics in a method
                > accessable from the class itself or just inside the method?[/color]
                Only the method, the above fields have a mangled name that is not valid
                within VB.NET.

                Of course using reflection you could access the fields.

                Hope this helps
                Jay

                "William Stacey" <staceyw@mvps.o rg> wrote in message
                news:%23DJ6i3Af DHA.1212@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                > I wonder why vb allows this? Hold over from vb6? Are statics in a method
                > accessable from the class itself or just inside the method?
                >
                > --
                > William Stacey, DNS MVP
                >
                > "Michael Culley" <mike@nospam.co m> wrote in message
                > news:OrulwsAfDH A.2332@TK2MSFTN GP12.phx.gbl...[color=green]
                > > vb.net allows this but not C# for some reason.
                > >
                > > "Action" <actionatadista nce@hotmail.com > wrote in message
                > > news:uKkehfAfDH A.2484@TK2MSFTN GP09.phx.gbl...[color=darkred]
                > > > in c++ if i put
                > > > static int count = 0;
                > > > in a method; count can only accessed inside the method and retain it's
                > > > value.
                > > >
                > > > is there equilvant thing in C#?
                > > > thx
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                Working...