When should I instantiate a class?

Asked

Viewed 346 times

5

I’m having second thoughts about the best time to call for instantiate a classe.

Should I do it at the beginning, before the builder, at the constructor or at the time when we will make use of some method?

In the example below I create instances of some classe and some Datatable, before my builder:

public partial class Manutenção_cliente : DevExpress.XtraEditors.XtraForm
{
    consulta_bd consulta_bd = new consulta_bd();
    cadastro_bd cadastro_bd = new cadastro_bd();
    excluir_bd excluir_bd = new excluir_bd();
    controles_text control_text = new controles_text();
    DataTable cobranca = new DataTable();
    DataTable entrega = new DataTable();
    Consulta_cidade cidade = new Consulta_cidade();

    public int id_cliente, editar, chek_excluir, chek_new_cli, contador, id_end_cobranca, id_end_entrega, ck = 0, lad = 0;

    public Manutenção_cliente(int id)
    {

1 answer

5


Classes are not called, only methods are called. At most it is calling the constructor that instantiates the class and the object is stored in the variable, in general indirectly.

If you’re talking about where declare the variables that will sustain the instance, there is no predefined right place, it is used where you have to use it. It can be in the class, it can be within a method, if your lifespan must be local.

If it goes at the beginning of the class, in the middle, at the end, before or after something, if everything goes scattered, little or nothing matters. It’s a question of organization and taste. It’s very common to do it early on, but not everyone follows it, and there may be cases where it’s better to do it differently.

There is no technical requirement, the initialization order cannot be defined within the class, only within methods.

It is common to group all private members, then group the properties. Static members are also usually grouped. There are those who like it until using #region, but "most" prefer to avoid this.

If you need a lot of organization it’s likely that the class is doing too much stuff.

Several variables of the class Manutenção_cliente are being initialized when an instance of this class is created. It seems to be a wise decision for this case.

In some cases some members may need to be initialized within the builder, common when one needs a specific order or needs to initialize other parts of the instances that cannot be easily done in the variables (needs later member initialization), or some specific processing. In much rarer cases you may need to initialize in some "normal" method, but this is a danger because the class will possibly be in invalid state.

This code does not follow the C nomenclature standard#. It also uses public or at least internal members (standard visibility), which should normally be avoided, but not at all costs, need to know when to do or not.

  • "Some people even like it #region, but most people prefer to avoid that." Why?

  • 2

    @ramaral clica no link who has an answer that talks more in detail about this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.