0

I have the following partial classes:

public partial class FormInstance
{
    private string referenceField;

    public string Reference
    {
        get { return this.referenceField; }
        set { this.referenceField = value; }
    }
}

public partial class FormField
{
    private string fieldNameField;
    private string fieldValueField;

    public string FieldName
    {
        get { return this.fieldNameField; }
        set { this.fieldNameField = value; }
    }

    public string FieldValue
    {
        get { return this.fieldValueField; }
        set { this.fieldValueField = value; }
    }
}

public partial class EFormData
{
    private FormInstance formInstanceField;
    private FormField[] formDataField;

    public FormInstance EFormInstance
    {
        get { return this.formInstanceField; }
        set { this.formInstanceField = value; }
    }

    public FormField[] FormData
    {
        get { return this.eformDataField; }
        set { this.eformDataField = value; }
    }
}

I have a method as follows which has a parameter that is of the above class object type:

public int writeEformData(EformData formFields)
{
    object[] results = this.Invoke("writeEformData", new object[] {
                        formFields});
    return ((int)(results[0]));
}

In my code I'm trying to initialise the EformData object as follows:

EformData eformData = new EformData ();
eformData.EformInstance.Reference = "1234";
eformData.FormData[0].FieldName = "txt_name";
eformData.FormData[0].FieldValue = "John Doe";
eformData.FormData[1].FieldName = "txt_address";
eformData.FormData[1].FieldValue = "10 Acacia Ave";

int result = web.writeEformData(eformData);

but when I debug I get error on line 2 eformData.EformInstance.Reference = "1234";

An unhandled exception of type 'System.NullReferenceException' occurred

Additional information: Object reference not set to an instance of an object.

Whats the correct syntax to initialise the EformData object and assigning values as I'm trying to do above?

Uwe Keim
  • 38,279
  • 56
  • 171
  • 280
adam78
  • 8,930
  • 20
  • 79
  • 187

1 Answers1

0

replace

eformData.EformInstance.Reference = "1234";

with

 eformData.EFormInstance = new FormInstance() { Reference = "1234" };

..and you will have the same troulbe in the following lines. You have to create a instance of each object before you can set a value to its properties

eformData.FormData = new FormField[2];
eformData.FormData[0] = new FormField() { FieldName = "txt_name", FieldValue = "John Doe" };
fubo
  • 42,334
  • 17
  • 98
  • 130