Share objects in various ways

1

Hi, I have a problem when I try to share objects between forms. I have a ConfData object in which I have several attributes that I want to update between the different forms that I have. The code is as follows:

public partial class ConfigDataViewer : UserControl
{
    #region Atributos privados

    /// <summary>
    /// Creamos el objeto para asignar los valores de la interfaz a los datos internos
    /// </summary>
    ECCE_ConfigData ConfData = new ECCE_ConfigData();

    TcConfig oTcConfig = new TcConfig(ConfData); -> ERROR

    UvConfig oUvConfig = new UvConfig();

    #endregion Atributos privados

This ConfData object is passed to another Form so that it can update a series of values and obtain them from this control. This is the code of the other way

public partial class TcConfig : Form
{
    ECCE_ConfigData ConfData2 = new ECCE_ConfigData();
    AñadirTc oTc = new AñadirTc();
    int i = 0;

    public TcConfig(ECCE_ConfigData ConfData)
    {
        InitializeComponent();
        ConfData2 = ConfData;
    }

    public void buttonAñadirTc_Click(object sender, EventArgs e)
    {
        if (oTc.ShowDialog() == DialogResult.OK)
        {
            ConfData2.TcCnf.TcCnfArray[i] = new sTcCnf(Convert.ToByte(oTc.textBox_IdTc.Text), Convert.ToByte(oTc.textBox_TcType.Text), Convert.ToByte(oTc.textBox_NumUVsTc.Text), Convert.ToByte(oTc.textBox_NumUVsNormalizacion.Text), Convert.ToByte(oTc.textBox_IdUVNormalizacion.Text), Convert.ToByte(oTc.textBox_TcParamFlags.Text));
            i++;
         }
     }

This last code is correct. The problem is that the class of the object ConfData is not static and the error that puts me in the line that I have put ERROR is:

  

Error 1 A field initializer can not reference the field,   non-static method or property   'CSCI_ECCE_APP.UserControls.ConfigDataViewer.ConfData'

Does anyone know how I can solve this? I tried to make the class of the ConfData object static but it gives me a lot of errors and I prefer another solution. Thanks

    
asked by Xim123 15.02.2018 в 10:07
source

1 answer

0

This error is caused by trying to use an instance field to initialize another instance field. The solution is to initialize the fields within the constructor of the class. In your case, define the uninitialized fields:

TcConfig oTcConfig;

And then initialize them in the constructor:

oTcConfig = new TcConfig(ConfData);

On the MSDN page Compiler Error CS0236 gives a simplified example of the problem :

public class MiClase
{  
    public int i = 5;  
    public int j = i;  // Error
}  

One possible explanation is the following. The compiler, on occasion, for optimization or other reasons, could change the order of the definitions. In that case, using the example, int j would try to initialize before int i , which obviously would cause an error since i is not yet defined. So the microsoft solution is not to allow this type of definition directly.

    
answered by 15.02.2018 в 11:50