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

static variables in C#?

Ant
Hi,
Can you declare a variable as static in C#? If not, then is there a 'module'
level area or eqivelant in C# in which you can do this?

Thanks in advance

Ant
Nov 17 '05 #1
3 1290
Hi Ant

static variable name;
On Sun, 26 Jun 2005 14:05:01 +0200, Ant <An*@discussions.microsoft.com> wrote:
Hi,
Can you declare a variable as static in C#? If not, then is there a 'module'
level area or eqivelant in C# in which you can do this?

Thanks in advance

Ant


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2
Ant
Hi Morten,
Thanks again.

I tried both these but both were not accepted. This is in a button click
event.

Static int siMyStaticVar; // Error on Static (understandable - s not S)
static int siMyStaticVar2; // Error on int (?)

Thanks again. Getting into C# after VB6. I feel like I've been in an
accident & have lost all my motor skills :)

Cheers
Ant

"Morten Wennevik" wrote:
Hi Ant

static variable name;
On Sun, 26 Jun 2005 14:05:01 +0200, Ant <An*@discussions.microsoft.com> wrote:
Hi,
Can you declare a variable as static in C#? If not, then is there a 'module'
level area or eqivelant in C# in which you can do this?

Thanks in advance

Ant


--
Happy coding!
Morten Wennevik [C# MVP]

Nov 17 '05 #3
Ant <An*@discussions.microsoft.com> wrote:
I tried both these but both were not accepted. This is in a button click
event.

Static int siMyStaticVar; // Error on Static (understandable - s not S)
static int siMyStaticVar2; // Error on int (?)

Thanks again. Getting into C# after VB6. I feel like I've been in an
accident & have lost all my motor skills :)


Are you trying to declare these as *local* variables? If so, there's no
direct equivalent to VB's local static variables in C#. Instead, you
need to make the variable part of the class - either as a static
variable or an instance variable, depending on your needs.

The reasoning for this is that a method doesn't logically have state -
only an object (or a type) does.

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

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

Similar topics

1
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab,...
2
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
4
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
8
by: Simone Chiaretta | last post by:
I've a very strange behaveour related to a website we built: from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
16
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions -...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.