An IDL union maps to an ActiveX interface named with the following properties and methods:
One get/set property for each field, with the same name.
A get/set discriminator property that represents the discriminator value. The discriminator has the ActiveX datatype that corresponds to the IDL discriminator type.
To set a union member, simply set the property for that member. The discriminator value changes automatically to match the member you set.
To access the value of a member, first verify that the discriminator value is in the set of allowable cases for that member, then reference the matching property. The ActiveX proxy throws an exception if you attempt to access a member while the discriminator value is not in the set of case values for that member.
Explicit version implicit field initialization Union fields that use complex types such as struct, union, object, date, time, or timestamp must be initialized explicitly. If you do not initialize these fields before passing the union as an EAServer method parameter or return value, the ActiveX dispatcher throws a marshalling exception. Fields of other types are implicitly set to a default value.
As an example, consider the following IDL union:
module TMod { union TUnion switch (long) { case 5: long lVal; case 9, 7: short sVal; default: double dVal; };};
The following Visual Basic code sets each member:
dim myUnion as TUnion set myUnion = new TUnion myUnion.lVal = 43000 myUnion.sVal = 43 myUnion.dVal = 43.43
The following code checks the discriminator and accesses the value if the lVal member is set:
if (myUnion.discriminator = 5) then print "Current value is " & myUnion.lVal endif
Copyright © 2005. Sybase Inc. All rights reserved. |