"Global" variabel for use with static method

  • Thread starter Thread starter Paw Pedersen
  • Start date Start date
P

Paw Pedersen

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
 
Paw Pedersen said:
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.
 
Paw Pedersen said:
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;
}
}
 
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
 
Bob Grommes said:
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 said:
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.
 
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
 
Bob Grommes said:
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.
 
Back
Top