473,698 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Global" variabel for use with static method

Is there a way to save a variabel that can be access from a static method?
I hope there would be some way to save it in memory so I don't have to save
it in a file. It's only for a few minutes the information have to be stored.
It's a simple static method that will be call with a guid in the input
parameter and return a integer telling how many times it have been called
with exactly this guid. After 1 minute it will not be called with the same
guid again.
I know I can solve it by saving the info to a file or database but then I
have to implement some kind of cleanup, since the method don't know when
it's the last time it has been called with a specific guid.

Regards Paw
Nov 16 '05 #1
8 4061

"Paw Pedersen" <ne**@paws.dk > wrote in message
news:uE******** ******@TK2MSFTN GP15.phx.gbl...
Is there a way to save a variabel that can be access from a static method?
I hope there would be some way to save it in memory so I don't have to
save
it in a file. It's only for a few minutes the information have to be
stored.
It's a simple static method that will be call with a guid in the input
parameter and return a integer telling how many times it have been called
with exactly this guid. After 1 minute it will not be called with the same
guid again.
I know I can solve it by saving the info to a file or database but then I
have to implement some kind of cleanup, since the method don't know when
it's the last time it has been called with a specific guid.


Wouldn't a static field do what you want? Probably a hashtable:

public static Hashtable guidTable = new Hashtable();

public static int GetGuidCount(Gu id guid)
{
object o = guidTable[guid];
if (o == null)
{
guildTable[guid] = 1;
return 1;
}
else
{
int count = (int)o);
count++;
guidTable[guid] = count;
return count;
}
}

Note that the above code would not be advisable in production. Its not
threadsafe and ptentially has other problems, its just an example.
Nov 16 '05 #2
Paw Pedersen <ne**@paws.dk > wrote:
Is there a way to save a variabel that can be access from a static method?


You could use a static variable:

class Foo
{
static int counter = 0;

public static void IncrementCounte r() {
++counter;
}
}
Nov 16 '05 #3
To clarify, that's not really a static variable. It's referred to as a
static class field or static class member. This is more than picky
semantics, because using the correct terminology helps to fix in your mind
(and the mind of whoever you're discussing it with) exactly what's going on.

A static variable doesn't really exist in C#, but in languages where it's
implemented, it would be scoped to a function, not to a class, and would
only be visible to the code in the function that uses it. I suppose that
somewhere there might be an OOP language that has static variables scoped to
methods, too, though I don't know for sure.

But the general idea is similar even if OOP languages usually come at it
from a different direction: having a variable retain its value in between
invocations of the code that uses the variable.

--Bob

"C# Learner" <cs****@learner .here> wrote in message
news:mk******** *******@csharp. learner...
Paw Pedersen <ne**@paws.dk > wrote:
Is there a way to save a variabel that can be access from a static
method?


You could use a static variable:

class Foo
{
static int counter = 0;

public static void IncrementCounte r() {
++counter;
}
}

Nov 16 '05 #4
Bob Grommes <bo*@bobgrommes .com> wrote:
To clarify, that's not really a static variable.


I know; I used the wrong word. I'll be more careful in future.
Nov 16 '05 #5
Bob Grommes <bo*@bobgrommes .com> wrote:
To clarify, that's not really a static variable. It's referred to as a
static class field or static class member.


The C# Language Specification disagrees. From section 12.1.1 (ECMA
numbering):

<quote>
A field declared with the static modifier is called a static variable.
</quote>

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote:
Bob Grommes <bo*@bobgrommes .com> wrote:
To clarify, that's not really a static variable. It's referred to as a
static class field or static class member.


The C# Language Specification disagrees. From section 12.1.1 (ECMA
numbering):

<quote>
A field declared with the static modifier is called a static variable.
</quote>


Interesting -- I used the phrase by mistake, not realising that it was in
fact technically correct here. I'll remember to consult the language
specification in future.

Thanks for the info.
Nov 16 '05 #7
In which case I would say that it was ill advised to enshrine that term in
the standard.

Oh well. Thanks for pointing it out.

--Bob

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Bob Grommes <bo*@bobgrommes .com> wrote:
To clarify, that's not really a static variable. It's referred to as a
static class field or static class member.


The C# Language Specification disagrees. From section 12.1.1 (ECMA
numbering):

<quote>
A field declared with the static modifier is called a static variable.
</quote>

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

Nov 16 '05 #8
Bob Grommes <bo*@bobgrommes .com> wrote:
In which case I would say that it was ill advised to enshrine that term in
the standard.


I disagree. Is it a variable? Yes. Is it static? Yes. I see no reason
not to describe it as a static variable. The only problem is when
people assume that "static variable" in C# means exactly the same as it
does in C/C++. I view that as a wider problem - there are quite a few
things which aren't going to mean exactly the same thing in the two
languages. Anyone who assumes that the terminology from C/C++ is going
to apply absolutely faithfully to any other language (not just C#) is
going to have a shock.

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

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

Similar topics

5
3491
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file scope. If you have a variable whose scope is file scope in another translation unit, you have to provide a local declaration to access that variable from the other translation unit. Also, I don't see "global variable" used once in the standard....
4
3534
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public myVar as string, etc. It seems to me that the scope and duration are the same, as they both are there while the application is running, and both go away when it quits. I presume that one difference is that the application state can be "flushed," such...
8
8029
by: Hans Greif | last post by:
Hallo, hello, i try to implement a global dataset - singleton based. On www.bakterienforum.de i found a singleton implementation: sealed class SingletonCounter { public int Counter = 0;
5
4934
by: dave | last post by:
If I have a class that hold, for instance, user settings that should be accessible to the entire program logic, what is a good paradigm to use? In C++, I would have made it a global object, protected if necessary for thread safety. In C#, of course, there are no global objects. The Program object is static, so cannot contain object instances. While I could store it in my main form pass it around, that seems cumbersome. I could do a...
10
6931
by: Bub.Paulson | last post by:
A month ago I finally took the plunge and began learning C# and ASP.Net, coming from a Classic ASP and VBScript background. In my classic ASP, I had my own little library of code that I stuck in an include file called "Common_VBscript.asp" which had all of the common stuff I used. (Constants, Connection Strings, little utlities like "email validation" functions, or functions to ready strings for db entry, etc.) What is the best way...
11
3158
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global" storage is ButtonHasBeenClicked. In this simple example code in Form1 calls a routine in Module1 which then calls code back in Form1 (subroutine WhatEver). WhatEver needs to access ButtonHasBeenClicked but the reference to ButtonHasBeenClicked...
7
1831
by: twang090 | last post by:
I find in other team member's project, they are referencing a type in following format " public static global::ErrorReport.Description Description = new global::ErrorReport.Description(); " I do not understand the usage of "global" here, could anyone please clarify its usage and what benift of using that. Thanks in advance.
1
3383
by: Tom Berger | last post by:
Just a short question concerning exception handling.... All of my applications contain an event handling in the Main() like this one: static void Main() { // declare global exeption handler Application.ThreadException += new
1
3682
by: sap0321 | last post by:
This should be kindergarten stuff but for some reason I am having trouble with it. I do 99.9% web programming and this is the first windows app i've done in years - probably the first ever in C# ... anyway ... Here is what I am trying to do .... Windows app - with a main "navigation" form with 4 linkbuttons on it - each of them open a child form with a task to perform ... on the main form, there is a Label control for the "Active...
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.