Search code examples
c#nullinfopathpartial-classes

C# partial class returns null


I'm trying to create populate an infopath template and upload it to sharepoint. I've been following these instructions https://www.codeproject.com/Articles/33228/Programmatically-create-a-browser-enabled-InfoPath

Here is a sample of the generated code that I have

public partial class myFields:

...
public partial class myFields
{ 
    private General_Information general_InformationField;
    ...
    public General_Information General_Information
    {
        get
        {
            return this.general_InformationField;
        }
        set
        {
            this.general_InformationField = value;
        }
    }
    ...
}
...

public partial class General_Information

public partial class General_Information
{

    private string wellField;
    ...

    public string Well
    {
        get
        {
            return this.wellField;
        }
        set
        {
            this.wellField = value;
        }
    }
    ...

The instructions populate the data this:

myFields fields = new myFields();
fields.Name = "Joe";
fields.Surname = "Blogs";

When I tried to follow the example in the website:

myFields fields = new myFields();
fields.General_Information.Well = "name";

I get an error saying myFields.General_Information.**get** returned null.

What is the proper way to do this?


Solution

  • fields.General_Information.Well = "name"; should be below. You forgot to instantiate the General_Information type and thus it's null. Moreover, not sure what's the need of making them as partial class. Shouldn't be actually.

    fields.General_Information gi = new General_Information();
    gi.Well = "name";