473,385 Members | 1,409 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,385 software developers and data experts.

static member declaration

Can someone please tell me how to declare a static variable in a function?
The following generates a compiler error:

public class MyClass
{
public void MyFunc()
{
static int i; // Compiler error: Invalid expression term 'static'
}
}
Nov 16 '05 #1
5 1714
You can't have a static variable inside a function, and I'm not sure I
understand why you would want one. You can place a static variable inside
the class, so the following class would work:

public class MyClass
{
static int i;
public void MyFunc()
{
//Do Something
}
}

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

"Ben S. Terry" <fake_email@spammers_suck.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Can someone please tell me how to declare a static variable in a function?
The following generates a compiler error:

public class MyClass
{
public void MyFunc()
{
static int i; // Compiler error: Invalid expression term 'static'
}
}

Nov 16 '05 #2
public class MyClass
{
private static int myInt;

public MyClass()
{
myInt = 0;
}
}

"Ben S. Terry" <fake_email@spammers_suck.com> wrote in message
news:#0**************@TK2MSFTNGP12.phx.gbl...
Can someone please tell me how to declare a static variable in a function?
The following generates a compiler error:

public class MyClass
{
public void MyFunc()
{
static int i; // Compiler error: Invalid expression term 'static'
}
}

Nov 16 '05 #3
Why doesn't CSharp allow static variables within the scope of a single
function? I realize that I can create a static variable within the class,
however I have a variable specific to only one function--why prohibit that?

"Ben Lucas" <be*@nospam.solien.nospam.com> wrote in message
news:G9********************@comcast.com...
You can't have a static variable inside a function, and I'm not sure I
understand why you would want one. You can place a static variable inside
the class, so the following class would work:

public class MyClass
{
static int i;
public void MyFunc()
{
//Do Something
}
}

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

"Ben S. Terry" <fake_email@spammers_suck.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Can someone please tell me how to declare a static variable in a function? The following generates a compiler error:

public class MyClass
{
public void MyFunc()
{
static int i; // Compiler error: Invalid expression term 'static' }
}


Nov 16 '05 #4
Ben S. Terry <fake_email@spammers_suck.com> wrote:
Why doesn't CSharp allow static variables within the scope of a single
function? I realize that I can create a static variable within the class,
however I have a variable specific to only one function--why prohibit that?


Because methods don't conceptually have state between invocations -
types and objects do.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
That makes sense. Thanks.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ben S. Terry <fake_email@spammers_suck.com> wrote:
Why doesn't CSharp allow static variables within the scope of a single
function? I realize that I can create a static variable within the class, however I have a variable specific to only one function--why prohibit
that?
Because methods don't conceptually have state between invocations -
types and objects do.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
14
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
1
by: Frederiek | last post by:
Hi, When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. Does that mean that the address of...
9
by: wizwx | last post by:
There are two typical implementations of a singleton. The first one is to use a static pointer class Singleton { static Singleton * pSingleton; public: static Singleton * instance() {...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
5
by: desktop | last post by:
Why is this struct illegal: #include<iostream> struct debug { std::string d1 = "bob\n"; }; I get this error:
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.