0

I have classes like that:

    public partial class InvoiceType
    {

    private UBLVersionIDType uBLVersionIDField;

    private CopyIndicatorType copyIndicatorField;

    private CustomerPartyType accountingCustomerPartyField;
    }

    public partial class CustomerPartyType
    {

        private PartyType partyField;

        private ContactType deliveryContactField;

        /// <remarks/>
        public PartyType Party
        {
            get
            {
                return this.partyField;
            }
            set
            {
                this.partyField = value;
            }
        }

        /// <remarks/>
        public ContactType DeliveryContact
        {
            get
            {
                return this.deliveryContactField;
            }
            set
            {
                this.deliveryContactField = value;
            }
        }
    }

When I want to generate a new InvoiceType I use that code:

        var invoice = new InvoiceType()
        {
            UBLVersionID = new UBLVersionIDType() { Value = "2.1" },
            CopyIndicator = new CopyIndicatorType() { Value = false },
        };

If I want to try to set

invoice.AccountingCustomerParty.Party.PartyIdentification = myPartyIdentification;

it occurs NullReferenceException. Because I didn't create AccountingCustomerParty and Party and also PartyIdentification yet. I must to create them with new keyword for first time to set value.

I can skip these steps with deserialize a draft invoice json at first, then all properties are created but I don't want to use deserialize method because it creates all properties. I want to create just my used properties.

Is there another way from deserialization method to set sub classes and their properties without using new keyword?

Omer Faruk KAYA
  • 342
  • 2
  • 10
  • 2
    You don't have any _**subclasses**_, what you have are classes _composed_ of instances of other classes. You may want to add constructors to your classes to initialize those properties (/fields). You also should look up _auto-properties_, they make your code easier to read and easier to write – Flydog57 Jun 07 '20 at 17:31
  • Does this answer your question? [deserialize part of json string (array) in c#](https://stackoverflow.com/questions/37335030/deserialize-part-of-json-string-array-in-c-sharp) – EJoshuaS - Stand with Ukraine Jun 07 '20 at 17:40
  • Thanks @EJoshuaS-ReinstateMonica, not exactly, because it may be changed for conditions. For example I want to set Results.GuestValues.A value if Today is Monday, Results.GuestValues.B if Today is Tuesday etc. Trying to partional deserialize can make it more complex I think. – Omer Faruk KAYA Jun 07 '20 at 17:45

2 Answers2

0

Create a constructor of CustomerPartyType & initialize Party.

public class CustomerPartyType
{
   //......

   public CustomerPartyType()
   {
     this.Party = new Party();
   }
}

Another way could be to initialize the private member in the setter of the property,

public class CustomerPartyType
{
   //......
    public PartyType Party
    {
        get
        {
            return this.partyField;
        }
        set
        {
            this.partyField = new PartyType ();
            this.partyField = value;
        }
    }
}
user366312
  • 16,949
  • 65
  • 235
  • 452
  • Thanks very much. If I use constructors, I must create(set default values to) all properties. But maybe I want to create just one property of class. So, other properties will not be created and will not come to serialization text. I want that a property will be created if I use it. – Omer Faruk KAYA Jun 07 '20 at 17:41
  • @OmerFarukKAYA, then, initialize `partyField` in the property `Party`. – user366312 Jun 07 '20 at 17:42
  • I'm not quite sure why you believe that you need to initialize everything in a constructor. But, even if you do, you can initialize things to `null`. You could also do something like `public PartyType Party {get; set;} = new PartyType ();` in the declarations of your properties – Flydog57 Jun 07 '20 at 18:35
0

Create a consturctor for CustomerPartyType and set default value to Party .

public class CustomerPartyType
{
   public CustomerPartyType()
   {
     this.Party = new Party();
   }
}
  • Thanks very much. If I use constructors, I must create(set default values to) all properties. But maybe I want to create just one property of class. So, other properties will not be created and will not come to serialization text. I want that a property will be created if I use it. – Omer Faruk KAYA Jun 07 '20 at 17:40