Help with static members

  • Thread starter Thread starter RP
  • Start date Start date
R

RP

I completely understand that a static member belongs to the type and is shared between instances of that type in an application scope.

So my question is really, what constitutes an application scope?

Let's say that I have a windows form application made up of several classes and one of those classes, StaticExampleClass, has a static member called SharedMember.

Example:

public class StaticExampleClass
{
public static int SharedMember = 0;
}

If I create 2 instances of the StaticExampleClass in my application there is only one value for SharedMember because it is static.

What happens if I start up another instance of the application? Now I have two winforms running on the same machine at the same time each with two instances of StaticExampleClass. Do all four instances of StaticExampleClass share the same value for SharedMember?


--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-
 
What happens if I start up another instance of the application? Now I have
two winforms running on the same machine at the same time each with two
instances of StaticExampleClass. Do all four instances of StaticExampleClass
share the same value for SharedMember?

No, you will have two instances of SharedMember, one for each instance of
the app.

As I understand it, a shared field is unique to the AppDomain where the
class is loaded. A simple app will have one AppDomain, but more than one is
possible, and thus it is possible to have more than one instance SharedMember
in one running app.
 
AMercer said:
two winforms running on the same machine at the same time each with
two instances of StaticExampleClass. Do all four instances of
StaticExampleClass share the same value for SharedMember?

No, you will have two instances of SharedMember, one for each
instance of the app.

As I understand it, a shared field is unique to the AppDomain where
the class is loaded. A simple app will have one AppDomain, but more
than one is possible, and thus it is possible to have more than one
instance SharedMember in one running app.

That is right, but with managed C++ in VS2005 you *can* have global
objects (ie not just static members, but actual objects created at
'application scope') that extend the behaviour of static members. The
defaults are:

/clr (mixed mode) there's a copy of the global object for the entire
process
/clr:pure (IL only) and /clr:safe (IL only and verifiable) there's a
copy of the global object per appdomain

You can use __declspec(appdomain) and __declspec(process) to change this
behaviour.

If the OP wants data to be global to a machine then the best way IMO
would be to use a shared memory section (memory mapped file) and use
this to hold a serialised form of the object's state. The .NET
performance counter classes do this (to make performance counters
available to the perfmon process), but unfortunately Microsoft don't
expose the classes they use to do this.

Richard
 
Back
Top