CompoundObjects
Compound Objects
Lets say there is a PhoneNumber Compound Object.
public class PhoneNumber
{
string CountryCode;
string AreaCode;
string Number;
string Extension;
}
A Person has two phone numbers:
public class Person
{
string Name;
...
PhoneNumber Tel;
PhoneNumber? Fax;
}
Tel is not nullabe, Fax is nullable.
Accessing Compound Objects
- If a compound object property is not nullable then the content of the property is always a valid reference.
- If a compound object property is nullable then the content of the property is always a valid reference, but all of the member may be null.
- When a compound object property is set the given compound object will be copied.
Person p;
string number;
number = p.Tel.Number;
number = p.Fax.Number; // number may be null, Fax is always valid
p.Tel.Number = "12345678";
p.Fax.Number = "12345678"; // Fax is always valid
PhoneNumber n;
n = p.Tel; // returns a reference of the compound object
n.Number = "87654321"; // changes
p.Tel.Number
n = p.Fax; // always valid
n.Number = "87654321"; // changes p.Fax.Number
p.Fax = p.Tel; // creates a copy of p.Tel
p.Fax.Number = "87654321"; // changes p.Fax.Number but does not change p.Tel.Number
p.Tel = null; // throws a ArgumentNullException
p.Fax = null; // throws a ArgumentNullException
n = ctx.CreateStruct<PhoneNumber>(); // creates a new PhoneNumber Struct;
n.Number = "12345678";
p.Tel = n; // creates a copy of n
p.Tel.Number = "18273645"; // changes p.Tel.Number but does not change n.Number
n.Number = "87654321"; // changes n.Number but does not change p.Tel.Number