473,386 Members | 1,715 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.

"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 4046

"Paw Pedersen" <ne**@paws.dk> wrote in message
news:uE**************@TK2MSFTNGP15.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(Guid 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 IncrementCounter() {
++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 IncrementCounter() {
++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.com>
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.com> 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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.com>
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.com>
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
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...
4
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...
8
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
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,...
10
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...
11
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"...
7
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...
1
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...
1
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# ......
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
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...
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
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,...
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.