473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 8204

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: lallous | last post by:
Hello, Why is the static variable inside a member function static for all instances and not static for the current instance of the object? -- Elias
2
by: ik | last post by:
Hello All, I am facing a problem as follows. I have a header file called myNameSpace.h which as the following contents. //Header file .. myNameSpace.h namespace myNameSpace { static int...
6
by: The8thSense | last post by:
how to declare static varible and static method inside a class ? i try "public static ABC as integer = 10" and it said my declaration is invalid Thanks
5
by: John Kelsey | last post by:
Back in the "old" C/C++ days, I used to declare static variables inside functions. Something like... // just a silly example to demonstrate the technique int foo(void) { static int NextVal =...
16
by: cmk128 | last post by:
Hi Why class static variable can't be public? thanks from Peter
18
by: Jack | last post by:
Thanks.
4
dmjpro
by: dmjpro | last post by:
I m from C and C++ programming world. There i saw that a static variable in a method lives between two function calls. Right?? But in JAVA it is valid. Look at this code carefully..... class...
1
by: kiran280603 | last post by:
hi i have a class in which i have a structure.. how to initialize a static variable "var" Class A { struct start { static int var; };
2
by: drmario | last post by:
Using Microsoft VC++2008 Windows XP I don't understand, for all the reading I've just done on the subject, what the difference there is. I mean if I declare a variable with global (file) scope,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.