Working with SEQUENCE OF X (Arrays)

SEQUENCE OF x (also called an "array") is represented in the generated code by a class derived from C# List<T>, where T is the type of the elements in the array.

Example

MyModule DEFINITIONS AUTOMATIC TAGS ::= BEGIN 
    Shelf ::= SEQUENCE { width INTEGER, items Books } 
    Books ::= SEQUENCE OF Book 
    Book  ::= SEQUENCE { title UTF8String, pages INTEGER } 
END

Here is the sample code that shows how to use the Shelf and Books classes:

// Create the array object (SEQUENCE OF Book) 
var myBooks = new Proj.MyModule.Books(); 

// Create and add two elements 
// First  
var hp = new Proj.MyModule.Book(); 
hp.Title = "Harry Potter"; 
hp.Pages = 870; 
books.Add(hp); 

// Second (using class initializers)  
books.Add(new MyModule.Book() { Title = "Animal Farm", Pages = 102 }); 

// Create and populate the top level object 
var myShelf = new Proj.MyModule.Shelf(); 
myShelf.Width  = 120; 
myShelf.Items = myBooks; // *v3: myShelf.Items.SetValue(myBooks);

// Loop through items  
for (i=0; i < myShelf.Items.Count; i++) // use Count property
System.Console.WriteLine(myShelf.Items[i]); // index access to Items

// Alternative way to enumerate items (available only for version 4) 
foreach (var b in myShelf.Items) 
System.Console.WriteLine(b);

NOTE: When the data defined in an ASN.1 schema becomes more complex (includes multiple levels, which result in multiple types defined in the generated code, also known as data bindings), the OSS ASN.1/C# Tools version 4 provides a more efficient method of managing objects owned by the application. In version 4, the application explicitly creates instances of all objects, including nested objects, whereas in version 3, the application explicitly creates an instance of the top level object, and the library implicitly creates instances of nested objects.

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# | Arrays | SEQUENCE OF | 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, 4G 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.