views:

212

answers:

2

I have a VB.NET application where various objects are going to access some common code, and I have some counters and values shared between all the calls, so I'm currently using a "Shared Class" (I'm aware classes can't be shared, per se, but all the variables and methods are marked "Shared").

My concern is for the visibility of this object - if a user runs two instances of the application on the same desktop, will they use the same "instance" of the shared class? In other words, will the two copies of the application both increment the same counters and use the same variables?

In addition, what about two user spaces on the same machine - if each user runs a copy, will those two copies interfere?

Perhaps I'm bastardizing the "Shared" concept and there's a much better way to do this (I know some people do shared code through Modules instead of classes). Is there a better method of having objects (both variables and methods) shared across the entire application, but kept separate from other application instances?

UPDATE: In response to the question about my "preferred" way, the application I'm writing needs keep data private, even between different instances that the user is running. In that case, I don't want to share any data at all between multiple instances, which is why I'm concerned about the side-effects of using a Shared class or a Module. Thanks for the question.

+3  A: 

Each instance of your application, whether run by a single user or multiple, will see its own copy of Shared fields.

This blog entry goes into more detail, describing cases where static/Shared fields are scoped to items like threads rather than the default context, an AppDomain.

Michael Petrotta
+3  A: 

Shared values (or Static if you are a C#er) are only common to the App Domain.

It is even possible to load multiple App Domains into the same process and each will have its own set of Shared values.

AnthonyWJones