Handling of optional | 0-1 lists

Jan 5, 2010 at 8:02 PM

Using V3.0.1.0 I have been unable to see how to get handling of optional attributes and elements which have a 0-1 size.

Recently added 'Lazy loading' alleviates some of the issues with construction of optionals, but because the field is not accessible you cannot check if it exists yet.

private List<dvd> dvdsField; 
public List<dvd> Dvds {
get {
if ((this.dvdsField == null)) {
this.dvdsField = new List<dvd>();
return this.dvdsField;
set {
this.dvdsField = value;

Some other providers have added a second field (bit ugly) to identify existance

public Boolean dvdsFieldExists { get{}; }


I like the option provided by the CodeSynthesis XSD tool in C++ giving a generic class Optional<>

Taking the new CircularClassReference.xsd example and extending
<xs:schema xmlns:mstns="http://tempuri.org/CircularClassReference.xsd" elementFormDefault="qualified" targetNamespace="http://tempuri.org/CircularClassReference.xsd" id="CircularClassReference"
  <xs:complexType name="Circular">
      <xs:element name="Circular" type="mstns:Circular"/>
      <xs:element name="Recursion" type="mstns:Circular" minOccurs="0" maxOccurs="1"/>
      <xs:element ref="mstns:Root" minOccurs="0" maxOccurs="1"/>
      <xs:element name="count" type="xs:int"/>
    <xs:attribute name="Optional" use="optional" type="xs:boolean"/>
  <xs:element name="Root" type="mstns:Circular"/>