Initialize variables in the constructor

0

I'm initializing variables in the constructor, but I'm using UserControl the issue is that when I initialize variables after InitializeComponent (); I get an error that the object was not referenced, but when I initialize before InitializeComponent (); everything works fine.

Where variables are initialized in the constructor before or after InitializeComponent ();

 public partial class ucCategoria : UserControl, ICommandAction
{
    private ISaCategoria _repositoryCategoria;
    private ISaSubCategoria _repositorySubCategoria;

    public ucCategoria(ISaCategoria repositoryCategoria, ISaSubCategoria repositorySubCategoria)
    {
        _repositoryCategoria = repositoryCategoria;
        _repositorySubCategoria = repositorySubCategoria;
        InitializeComponent();
    }

There is a difference

public partial class ucCategoria : UserControl, ICommandAction
{
    private ISaCategoria _repositoryCategoria;
    private ISaSubCategoria _repositorySubCategoria;

    public ucCategoria(ISaCategoria repositoryCategoria, ISaSubCategoria repositorySubCategoria)
    {
        InitializeComponent();
        _repositoryCategoria = repositoryCategoria;
        _repositorySubCategoria = repositorySubCategoria;
    }

In the first case it works well for me. Note: I am working with Windows Forms.

    
asked by Pedro Ávila 21.05.2016 в 05:54
source

4 answers

1

The InitializeComponent method is usually created by the form designer (or in this case user controls) of Visual Studio and has the initialization code of the user control, everything that you have defined from the designer: add controls to the control of user, set properties of controls etc.

I understand that doing this initialization is calling some part of the code that needs these private variables to be initialized. That's why you need to initialize those variables before.

Beware, it is not that it is a general rule ("you must initialize the variables before calling the InitializeComponent"), it is that in your case it is necessary to do so. In other cases it may be necessary to do it later.

If you want to know which code is causing the error, you only have to debug and follow the call stack when the exception is generated.

    
answered by 21.05.2016 / 08:51
source
0

InitializeComponent (); what it does is, as the name implies, initialize the components of the UI. It should usually be after it returns, and so there will be no method that assigns any default value.

Look at this, if it is WPF:

link

    
answered by 21.05.2016 в 07:07
0

To avoid the error, it would not be more feasible to initialize the variables in the load method? so the data would be loaded before showing them to the user but the form would already be created so I would not have to throw you a good error that occurs to me

    
answered by 21.05.2016 в 17:14
0

The "InitializeComponent ();" component within the structure C # works to initialize all the parameters contained in the form, whether they are variables or constants, if you mark that error it is because the assignment of those values is missing in order to correctly execute all the data, that is why if they are values what you need for a first boot of your system you must put all the variable declarations before this component

I hope it worked for you, Regards

    
answered by 24.05.2016 в 19:52