I have a few forms, and a class Management which has a list of users, info and stuff. I want an instace of Management which I will be able to access from all the forms. How do I do that? Thenx in advance.
-
3Show your work and what have you tried.. – Soner Gönül Feb 21 '14 at 15:14
-
Are you using MVC or MVVM pattern ? – thomas Feb 21 '14 at 15:14
-
I'm not using MVC. First I had an instace of Management in Form1, and I had a second form, so I tried to send the instance using a function as a parameter, and as a return value of a function from Form1. I always get error like so: Error 3 Inconsistent accessibility: parameter type 'ProjectClasses.Management' is less accessible than method 'FinaleSystem.MenuForm.Start(ProjectClasses.Management)' D:\MAS_project\FinaleSystem\FinaleSystem\MenuForm.cs 50 21 FinaleSystem – user1729796 Feb 21 '14 at 15:18
-
How can there be so many answers??? I don't even understand the question yet! – musefan Feb 21 '14 at 15:21
5 Answers
Best way (my point of view) is to use a MVVM pattern and have the ViewModels inherit from a base class

- 1,399
- 1
- 17
- 32
Just to elaborate on Thomas' answer.
Singleton
A singleton is basically a class where it only ever allows the program to holds one instance of itself. In other words, whether you're in the Superman
class or the Batman
class, the Singleton class, let's call it MyCar
will always be the same.
A Singleton is pretty easy to implement and to grasp. Take a look at this tutorial: http://www.usmaanz.com/singleton/ to get an idea.
MVVM
A MVVM pattern is pretty powerful! It allows you to create an object which contains certain amount of properties and allow that model to be used by many Views or Forms in your case.
Let's say that a Form has the following controls:
- Username
- Password
And in this form, we wish to hold data from what is being passed in to these controls. Therefore, the following class will help us hold that data:
public class MyModel
{
public string Name {get;set;}
public string Password {get;set;}
public string Email {get;set;}
}
Then in your Form, you may do:
MyModel model = new MyModel(){Name = txtName.Text, Password = txtPassword.Text, Email = txtEmail.txt};
This object will now hold the data of the form. You may also use this class to hold data any where else and you can obviously, freely, create as many instances of it as you want.
Hope that helps!

- 5,370
- 15
- 70
- 125
-
Thenx, it will be a good reading, but I found an easier solution using what Oliver said. Thank you very much for your help any way :) – user1729796 Feb 21 '14 at 15:39
The error
Error 3 Inconsistent accessibility: parameter type 'ProjectClasses.Management' is less accessible than method 'FinaleSystem.MenuForm.Start(ProjectClasses.Management)'
means that your MenuForm
is exporting a method Start
(probably it is public
) having a parameter of type ProjectClasses.Management
that is less accessible. Probably it is internal
. Declaring the Management
class as public
will resolve your problem. If the class is nested within another class, declare the "parent" class as public
as well. If you prefer not to make the class public
, make the method Start
internal
instead.
public
means that an item is accessible from another project. internal
means that the item is only accessible within the same project. If Start
was public
and the type of a parameter internal
or private
you could not call the method from another project, since you could not create an object of the requested type. You couldn't derive a class from it either in order to use it as a parameter.
Non-nested classes have a default access modifier of internal
. Nested classes have a default access modifier of private
.
See https://stackoverflow.com/a/3763638/880990 for details

- 1
- 1

- 104,806
- 13
- 138
- 188
One possible solution is to make your Management class' properties static
.
Cheers

- 5,302
- 20
- 39
The simple case of this is implemented as a singleton where one and only one instance of a class exists for the life of the program. Singleton has many drawbacks mostly related to difficulty of testing and correctly handling threading. The next pass at solving this is usually implemented as a service locator pattern, however this has also come to be viewed as an anti-pattern. The best way to handle this is called dependency injection. While DI is the "best way" it may be hard/over kill in your scenario.

- 25,014
- 6
- 48
- 78