You Can Be Anything: Variants


In LabVIEW, you cannot connect wires of different types, unless the data source can be coerced to the data sink. For example, you cannot wire a string control to a DBL numeric indicator, because a string cannot be coerced to a DBL. However, you can wire an I32 numeric control to a DBL numeric indicator, because an I32 can be coerced to a DBL. This requirement that the data source must match or be coercible to the data sink is one that we take for granted. But, it is extremely important because it ensures that our VI runs properly. You certainly would not want LabVIEW to allow you to wire both a string data type and a numeric data type to an Add function (which makes no sense), and simply give you an error when the Add function executesthat would just waste your time. In general, you need to know about these type mismatch problems while you are editing your VI rather than when we are running your VI. LabVIEW's strict typing is one of its main advantages.

Sometimes, though, you may desire flexibility while wiring. You want to be able to wire different types (or possibly any type) because the particular code you are writing is required to handle many types of data (or possibly any type of data). For example, if you are writing a software component that routes messages between different parts of large software applications, you probably don't care about the type of data that is in the messageit's not your job to care about the type of data you are routing; it's your responsibility to know how to route the data to its destination.

LabVIEW has a very special data type that provides the flexibility and features needed for just this sort of situation: the variant data type. If you have programmed in Visual Basic before, you're already familiar with this "universal" data type.

Figure 14.61 shows three dissimilar data types converted to variants using the To Variant function (Programming>>Cluster & Variant palette) and then built into an array of variants.

Figure 14.61. Creating an array of variants allows you to create an array of mixed types (different data types inside each variant)


On the front panel, a variant control (found on the Modern>>Variant subpalette of the Controls palette) can display both the type and (simple) textual representation of the data. You can toggle the visibility of the data and type information from a variant's pop-up menu options Show Data and Show Type.

The functions for operating on variant data are found on the bottom two rows of the Programming>>Cluster & Variant palette (see Figure 14.62).

Figure 14.62. Cluster & Variant palette


To Variant (Programming>>Cluster & Variant palette) converts any LabVIEW data to variant data (see Figure 14.63).

Figure 14.63. To Variant


Variant To Data (Programming>>Cluster & Variant palette) converts variant data to a specific LabVIEW data type so LabVIEW can display or process the data (see Figure 14.64).

Figure 14.64. Variant To Data


Variant To Flattened String (Programming>>Cluster & Variant palette) converts variant data into a flattened string and an array of integers that represent the data type (see Figure 14.65).

Figure 14.65. Variant To Flattened String


Flattened String To Variant (Programming>>Cluster & Variant palette) converts flattened data into variant data (see Figure 14.66).

Figure 14.66. Flattened String To Variant


These functions allow you to convert LabVIEW data to variants and variants to LabVIEW data, but nothing more. They do not provide you with a means to inspect variant data, nor operate on it.

There are a limited set of tools that ship with LabVIEW for inspecting and operating on variant data. These are sort of "hidden" in LabVIEW (they are not found in the Functions palette), but you can find them in the following folder: vi.lib\Utility\VariantDataType.

Probably the most useful of these functions is GetTypeInfo.vi, which tells you both the type and name of variant data. This is very useful, because once you know the type of a variant, then you can use the Variant To Data function to convert it back into that type.

Get Type Info.vi (vi.lib\Utility\VariantDataType folder) returns information about the data type stored in a variant, including the data type (as an enum), the data name (as a string), and typedef info (see Figure 14.67).

Figure 14.67. Get Type Info.vi


Finally, one other useful feature of variants is their capacity to store additional information in the form of Variant Attributes. Variant attributes are key-value pairs of data that can contain any sort of information you wish. For example, if you want to know the time when a piece of data was created, you could store a "Time" attribute in the variant. The attributes of a variant can be any kind of data type and contain anything you want.

The Variant Attribute palette shown in Figure 14.68 has functions for reading, setting, and deleting variant attributes. The name of the attribute is a string, and the value of a variant attribute is . . . a variant itself!

Figure 14.68. Variant Attributes palette


Using Variants: Creating Generic Software Components

Let's look at an example that highlights the power and flexibility of variants. Figure 14.69 is a VI that formats an array of variants (anything) into a string of key-value pairs, like you might see in an configuration file (INI) file.

Figure 14.69. Block diagram of a VI that converts an array of variants into a multi-line string of key-value pairs


This end result is shown in Figure 14.70.

Figure 14.70. Front panel of a VI that converts an array of variants into a multi-line string of key-value pairs


You might be thinking to yourself that we have just done a whole lot of work in order to do something as simple as formatting a string from the values of three controls. Couldn't we simply whip up a VI that looks like Figure 14.71?

Figure 14.71. Block diagram showing an "easy" way to create a string of key-value pairs using Format Into String


Sure we could, and we would save a lot of time now. But, our variant implementation will save us time in the future. Here is how:

  • The variant implementation gets the data names programmatically from the variant data. In our "simple" example, we have to change the name in two locations. This is extra work, and we might make a mistake!

  • In our simple example, if we want to change the equal sign (=) to a colon (:), or another character, we have to do it once for every data element. This change requires work that is proportional to the number of elements.

  • Our simple example is limited to the three elements that are wired up to it. But, our variant example can easily handle any number of elements in the variant array. It could even be converted into a subVI (with an array of variants as an input parameter) and used in multiple locations of our code or even in multiple projects!

The moral of the story is that things that appear simple at first, might not be the best scaleable solution. By using variants, you can create scaleable, generic, powerful, and flexible tools.

If you are interested in doing much work with variants, you will benefit from a powerful set of tools for parsing type descriptors and flattened data. Just such a set of tools is available from OpenG.org. See Appendix C, "Open Source Tools for LabVIEW: OpenG," for more information.


ActiveX and the Variant Data Type

ActiveX is a Microsoft technology that allows you to reuse components of code in other applications. For example, a component could be the ADO object, which allows you to access almost any kind of database, or the Windows Media Player component, which allows you to play audio and video files. Even LabVIEW itself can become an ActiveX component. ActiveX is a fairly advanced topic, and we'll talk more about it in Chapter 16.

Figure 14.72. Variant to Data function


When exchanging data with ActiveX servers and controls, you will often pass (and be passed) data as a variant. However, data passed from ActiveX into LabVIEW is passed as an ActiveX variant and not a true LabVIEW variant (although the wire pattern and color look exactly the same). Do not attempt to operate on the variant data using variant utility functions and VIs (for example, Variant To Flattened String will fail); but rather, immediately convert the ActiveX variant into LabVIEW data using the Variant to Data function.

That's all we will say about ActiveX variants. Please keep this in mind as you explore ActiveX in Chapter 16.




LabVIEW for Everyone. Graphical Programming Made Easy and Fun
LabVIEW for Everyone: Graphical Programming Made Easy and Fun (3rd Edition)
ISBN: 0131856723
EAN: 2147483647
Year: 2006
Pages: 294

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net