469,593 Members | 1,823 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,593 developers. It's quick & easy.

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
Nov 15 '05 #1
7 7932

Yes there is a static modifier in c#. However you can't
declare a static variable inside a function.
-----Original Message-----
in c++ if i put
static int count = 0;
in a method; count can only accessed inside the method and retain it'svalue.

is there equilvant thing in C#?
thx
.

Nov 15 '05 #2
vb.net allows this but not C# for some reason.

"Action" <ac***************@hotmail.com> wrote in message
news:uK**************@TK2MSFTNGP09.phx.gbl...
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

Nov 15 '05 #3
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" <mi**@nospam.com> wrote in message
news:Or**************@TK2MSFTNGP12.phx.gbl...
vb.net allows this but not C# for some reason.

"Action" <ac***************@hotmail.com> wrote in message
news:uK**************@TK2MSFTNGP09.phx.gbl...
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


Nov 15 '05 #4
"William Stacey" <st*****@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I wonder why vb allows this? Hold over from vb6?
Yep, it's due to compatibility with vb6.
Are statics in a method
accessable from the class itself or just inside the method?


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
Nov 15 '05 #5
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
Yes there is a static modifier in c#. However you can't

declare a static variable inside a function.<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #6
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" <do*****@yahoo.fr> wrote in message
news:07****************************@phx.gbl...

Yes there is a static modifier in c#. However you can't
declare a static variable inside a function.
-----Original Message-----
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
.

Nov 15 '05 #7
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.
Are statics in a method
accessable from the class itself or just inside the method? 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" <st*****@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... 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" <mi**@nospam.com> wrote in message
news:Or**************@TK2MSFTNGP12.phx.gbl...
vb.net allows this but not C# for some reason.

"Action" <ac***************@hotmail.com> wrote in message
news:uK**************@TK2MSFTNGP09.phx.gbl...
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



Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by lallous | last post: by
16 posts views Thread by cmk128 | last post: by
18 posts views Thread by Jack | last post: by
dmjpro
4 posts views Thread by dmjpro | last post: by
2 posts views Thread by drmario | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.