Handling Variant Data

Sometimes you may need a VI to handle data of many different types in a generic way. You could write a different VI for each specific data type, but having copies of your VI is hard to maintain, since if you change one you have to remember to change all of them. LabVIEW provides the variant data type as one solution for these situations. The variant data type is a generic container for all other types of data in LabVIEW. When you convert other data to a variant, the variant stores the data and the original data type of the data, which allows LabVIEW to correctly convert the variant data back to the original data at a later time. For example, if you convert string data to a variant, the variant stores the text of the string and an indicator that says that this data was originally a string (as opposed to a path or an array of bytes, or other possible LabVIEW types).

Use the Variant functions to create and manipulate variant data. You can convert any LabVIEW data type to the variant data type to use variant data in other VIs and functions. Several properties and methods, such as the Get VI:ConPane DataType method and the Connector Pane:DataType property, return the data type as a variant. Use the Data Type Parsing VIs to retrieve the data type of a variant and type information about the data type. You also can check whether the data type of a variant matches a specific data type.

Use the variant data type when it is important to manipulate data independently of data type, such as when you transmit or store data; read and/or write to unknown devices; or perform operations on a heterogeneous set of controls.

For example, use the variant data type when working with ActiveX objects that require a variant data type. You also can use the variant data type with control references to read or modify values of a programmatically selected control.

You also can use the Flatten To String function to convert a data type to a string data type to represent data independently of type. Flattening data to strings is useful when you use TCP/IP to transmit data because the protocol understands only strings. However, using flattened data has limitations because LabVIEW cannot coerce flattened data when the original data type does not match the data type to which you want to convert. You cannot flatten ActiveX variant data. Also, attempting to unflatten a flattened integer as an extended-precision floating-point number fails.

Another advantage of using the variant data type is the ability to store attributes of the data. An attribute is information about the data that you define and the variant data type stores. For example, if you want to know the time when a piece of data was created, you can store the data as variant data and add an attribute called Time to store the time string. The attribute data can be of any type. Use variant attributes when you want to sort the data by a particular attribute, identify the device or application that generated the data, or filter the data for only those variants with a particular attribute.

Note�� If you use the Flatten To String function on variant data, LabVIEW will flatten the variant and all its contents, including attributes. However, if you use the Variant To Flattened String function, LabVIEW flattens only the variant and discards any attributes.