Handling Variant Data

LabVIEW 2013 Help

Edition Date: June 2013

Part Number: 371361K-01

»View Product Info

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 polymorphic functions return the variant 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.


Not Helpful