How do I select a CHOICE component?

The following example is from the manual, and also exists as a part of a sample in the sample directory. Shown first is field of a SEQUENCE in the ASN.1 definition.

operation [7] CHOICE
{
createReqt [0] CreateReqt,
updateReqt [1] UpdateReqt
}

The representation of a CHOICE type is a structure which includes a choice field and element fields (for each CHOICE alternative). In addition to these fields, #define constants are also defined for each alternative in the CHOICE type. For the ASN.1 above, the following is generated.

struct {
unsigned short choice;
# define createReqt_chosen 1
# define updateReqt_chosen 2

union {
CreateReqt createReqt; /* to choose, set choice to
* createReqt_chosen */
UpdateReqt updateReqt; /* to choose, set choice to
* updateReqt_chosen */
} u;
} operation;

To specify that a particular alternative is selected, all you have to do is set the choice field equal to the #define constant that corresponds to the selected alternative.

myMsg.operation.choice = createReqt_chosen;
myMsg.operation.u.createReqt.ordId = 5432;
memcpy(myMsg.operation.u.createReqt.action.value,          actionOctStr, 5);

myMsg.operation.u.createReqt.action.length = 5;
memcpy(myMsg.operation.u.createReqt.fldNm.value, fldNmOctStr,          15);
myMsg.operation.u.createReqt.fldNm.length = 15;
myMsg.operation.u.createReqt.creditChecked = TRUE;

For the complete example (including the definition of createReqt), please refer to the OSS ASN.1/C API Reference Manual (section 10.2 of the April 2011 edition).


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.