Working with OPTIONAL and DEFAULT Fields

OPTIONAL and DEFAULT fields are represented by C# nullable types (for example, int?, bool?). Nullable types can be set to null to indicate that a particular field is not present (in this case the field is also not encoded). For DEFAULT fields, null also indicates that the field value is the default one.

For convenience, default values are represented by an additional property prefixed by Default, for example DefaultCountry.

Example

MyModule DEFINITIONS AUTOMATIC TAGS ::= BEGIN

Address ::= SEQUENCE {
    street  UTF8String OPTIONAL,
    city    UTF8String,
    country UTF8String DEFAULT "USA"
    }
END

Here is the sample code:

// Create objects: a BER codec and two addresses
Proj.BerCodec codec = new Proj.BerCodec();
Proj.MyModule.Address basicAddr = new Proj.MyModule.Address();
Proj.MyModule.Address fullAddr = new Proj.MyModule.Address(); 

// Initialize without setting optional or default values (see note 1 below)
basicAddr.City = "Somerset"; // the only one required
Oss.Asn1.ValueNotationFormatter.Print(basicAddr, System.Console.Out); 

// Output: value Address ::= { city "Somerset" } 

// First encode: 
null properties are not encoded
byte[] ber = codec.Encode(basicAddr);
Oss.Asn1.ValueNotationFormatter.Print(ber, ber.Length, System.Console.Out); 

// Output: 300A8108 536F6D65 72736574 

// Second encode: include DEFAULT values (see note 2 below)

// Explicitly assign them 
basicAddr.Country = Proj.MyModule.Address.DefaultCountry; 
ber = codec.Encode(basicAddr); 
Oss.Asn1.ValueNotationFormatter.Print(ber, ber.Length, System.Console.Out); 

// Output: 3300F8108 536F6D65 72736574 82035553 41 

// Decode it into fullAddr codec.Decode(ber, fullAddr);
Oss.Asn1.ValueNotationFormatter.Print(fullAddr, System.Console.Out); 
                
// Output: value Address ::= { city "Somerset", country "USA" } 

// Check or set the optional (see note 3 below)
if (fullAddr.Street == null)
fullAddr.Street = "---"; 

// Check or set the default (see note 3 below)

if (fullAddr.Country == Proj.MyModule.Address.DefaultCountry)
 fullAddr.Country = null; 
                   
// Implicit defaults are not printed 
Oss.Asn1.ValueNotationFormatter.Print(fullAddr, System.Console.Out);

// Output: value Address ::= { street "---", city "Somerset" } 

// Enable printing of implicit defaults 
Oss.Asn1.ValueNotationFormatter.Options.PrintImpliedValues = true;
Oss.Asn1.ValueNotationFormatter.Print(fullAddr, System.Console.Out); 

// Output: value Address ::= { street "---", city "Somerset", country "USA" }

NOTE 1: Starting with OSS ASN.1 C# Tools version 4, default values are represented by null, which is a more efficient approach compared with the one provided by version 3, where default values are implicitly set by the object constructor.

NOTE 2: In version 3, default values are encoded using a special encoder option: encoder.SetProperty("encodingDefaultValues", "true").

NOTE 3: In version 3, to check if values are present, you must use the HasStreet() or HasCountry() method.

Download files for this article. Note that you must first compile the .asn file to use the generated files with the Program.cs file.

Tags:

C# | OPTIONAL | DEFAULT | Migration


The samples included with some of the Knowledge Center answers are meant for your general understanding of the OSS products. Different versions of the products might produce slightly different outputs. Consult the products documentation and samples for the most up-to-date products information and code examples.



Contact Support
contact Our office hours
24 hours/day, 7 days/week

  • Phone: 1-888-OSS-2761 (USA and Canada)
  • Phone: 1-732-302-9669 (International)
  • Fax: 1-732-302-0023
  • Email: support@oss.com
Free Trial
download

Test drive the OSS Nokalva ASN.1, LTE, and XML Tools now! Your trial includes complete software, documentation, sample programs, free 24x7 technical support and more.




Learn ASN.1
Learn ASN.1

Our expert personnel can help you learn ASN.1!

We offer 4-day ASN.1 courses at our headquarters or your premises.