EZ Jcom

Java to COM/ActiveX Bridge

Working with Variants in EZ JCom

COM contains a data type “VARIANT”, that can be used to hold various different types.  For instance, it could hold an integer, a real number, a string, an array, or a record.  It has a “variant type” field that describes what is being currently held in the variant.


EZ JCom provides a class ezjcom.JComVariant that corresponds to the VARIANT data type.


It is possible to call the method “getType()”and compare the returned value to various constants, or to call the method “setType()” with an appropriate constant.  In many cases, simpler Java-style approaches are available.


For instance, to create a new variant that holds an integer, simply use the constructor that accepts an integer, e.g.

  new ezjcom.JComVariant( 3 );

will create a variant with type “integer” and value 3, whereas

  new ezjcom.JComVariant( "abcd" );

will create a variant with type “string” and value “abcd”.


If you are required to pass in a variant to a COM object, you will need to know the expected type.


When you receive a variant from a COM object, you will need to know the type of the returned variant to work with this.  Often, the method JComVariant.getObject() is very useful.  It returns an object of the appropriate type (e.g. Integer if the variant is holding an integer.)  Because often variants returned by a COM object contain other COM interfaces, calling getObject() and printing out its class can help work further with the returned object.


JComVariant also has a “toString()” method, which can be used to print out a string representation of the variant.

Iterator Processing shows an example of dealing with variants returned by a COM method.