is there 'static' variable inside a method?

  • Thread starter Thread starter Action
  • Start date Start date
A

Action

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