Make global reference in C #

1

I am creating a simple system of operations with no database in C #. It has 3 Forms, which are: Login.cs , Register.cs and Main.cs . It also has the Conta.cs class, where operations are performed.

My question is this: in form Registration.cs I created a reference called mconta and I would like it to be global. In this case only Cadastro.cs has access to it.

Conta[] mconta;
mconta = new Conta[10];
    
asked by anonymous 23.12.2017 / 01:07

1 answer

2

There are several ways to do this, even there is a pattern called Singleton that could answer depending on your case, I'll just put a simple form, but I think it gets "messed up" there would have to see how your code is, organization, anyway ...

In the Account class itself, set a variable to public static :

public class Conta
{
    //Declare sua variável:
    public static Conta[] mConta = new Conta[10];
}

In any other class, you can only access it by the class name:

... Conta.mConta;

But I urge you to review your need, I do not think this is necessary. Another question is to use generic collections instead of array, it's much simpler and more practical:

List<Conta> mConta = new List<Conta>();
    
23.12.2017 / 13:27