A Word About Polymorphic VIs


By now you are comfortable with how to use and create subVIs including their connector pane. One concept we want to introduce you to is the polymorphic VI. Like the name implies, a polymorphic VI is capable of handling different data types on its input, by automatically adapting to the wired data type. Polymorphic VIs accept different data types for a single input or output terminal. A polymorphic VI is really just a collection of VIs (called members) having the same connector pane pattern. When a polymorphic VI is placed on the block diagram as a subVI, you only "see" one of the member VI icons at any given time.

With LabVIEW's primitive functions, such as the Add function, you are already familiar with how these functions are polymorphic. That is, you can wire scalars, arrays, or clusters to the Add function and it automatically will work with those data types.

Normally, when you create a subVI and wire its connector pane to the inputs and outputs, you are explicitly defining the data types it works with. So, if your subVI takes a numeric value as "input 1," you normally can't wire a string to "input 1" or you'll get a broken wire. But if a VI is created as a Polymorphic VI, you can define multiple types of data for the same subVI inputs and outputs, and you can create separate pieces of code to handle each. For example, the Autocorrelation function (from the Signal Processing>>Signal Operation palette) is a polymorphic VI. When you put it on the block diagram, you can wire either a 1D array or a 2D array to the "X" input (see Figures 14.1 and 14.2). Notice that wiring a 2D array causes one input to disappear and the output to also become a 2D array type.

Figure 14.1. AutoCorrelation VI with 1D array wired


Figure 14.2. AutoCorrelation VI with 2D array input wired


Pop up on a subVI that is polymorphic, and select Visible Items>>Polymorphic VI Selector to show the Polymorphic VI Selector (see Figure 14.3).

Figure 14.3. Polymorphic subVI with Polymorphic VI Selector visible


The Polymorphic VI Selector is a drop-down list showing all of the member VIs that belong to the polymorphic VI (see Figure 14.4).

Figure 14.4. Polymorphic VI Selector drop-down list of members


You will notice that the default setting for any polymorphic subVI is Automatic. This means that the subVI is configured to automatically adapt to the data type that is wired into it. However, by selecting a different member VI from the Polymorphic VI Selector, you will disable automatic selection and use the specific member that you have selected.

The Polymorphic VI Selector will display the member that is currently selected, whether it was chosen automatically or explicitly. You cannot tell how a polymorphic subVI has been configured simply by looking at it.


There is another way, beside the Polymorphic VI Selector, to configure (and view the configuration of) a polymorphic subVI. Pop up on the subVI and select any of the options beneath the Select Type>> submenu (see Figure 14.5). Note that these are the exact same options available from the Polymorphic VI Selector.

Figure 14.5. Configuring a polymorphic subVI from its pop-up menu


It's beyond the scope of this book to teach you how to make polymorphic VIs, but we wanted you to be aware of their existence, because many of LabVIEW's built-in functions are polymorphic VIs. For example, most of the DAQmx VIs you saw in Chapter 11, "Data Acquisition in LabVIEW," are polymorphic, as they allow you to read or write scalars, arrays, waveforms, and so on, with the same VI function. In this chapter, we'll look at Configuration VIs, polymorphic VIs for reading and writing configuration files.

To learn how to create your own polymorphic VIs, consult the following section of the LabVIEW Help Documentation: Fundamentals>>Creating VIs and SubVIs>>How-To>>Creating SubVIs>>Building Polymorphic VIs.




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