472,126 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 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 3998

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Hans Greif | last post: by
7 posts views Thread by twang090 | last post: by
1 post views Thread by Tom Berger | last post: by
reply views Thread by leo001 | last post: by

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.