user1729796
user1729796

Reputation: 61

How to access variables in a class from all forms

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.

Upvotes: 0

Views: 79

Answers (5)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112279

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

Upvotes: 1

Subby
Subby

Reputation: 5480

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
  • Email

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!

Upvotes: 1

rerun
rerun

Reputation: 25495

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.

Upvotes: 0

Luc Morin
Luc Morin

Reputation: 5380

One possible solution is to make your Management class' properties static.

Cheers

Upvotes: 0

thomas
thomas

Reputation: 1453

Best way (my point of view) is to use a MVVM pattern and have the ViewModels inherit from a base class

Upvotes: 1

Related Questions