Object-Oriented Programming in LabVIEW NXG

Überblick

Object-oriented (OO) programming has demonstrated its superiority over procedural programming for solving a variety of problems such as creating plug-in architectures. Object-oriented design encourages cleaner interfaces between sections of code and results in code that is easier to debug and scales better for large programming teams.

This white paper assumes basic familiarity with object-oriented concepts and reviews how to approach object-oriented programming in LabVIEW NXG.

In LabVIEW 2017 or earlier, type-defined controls (.ctl files) are used to create user-defined types, and classes (.lvclass files) are used to create a LabVIEW class. Both of these constructs are supported by G Types (.gtype files) in LabVIEW NXG.

LabVIEW classes are by-value rather than by-reference. In by-value syntax, each object has its own data values that you can modify independently of other objects. In by-reference syntax, there is a single instance of data to which multiple objects refer. If one object modifies the data, it must be careful not to conflict with other objects. By-value means that the wire carries actual data of the type of the wire.

Before creating a class, you may want to define a custom type, or type definition, to use throughout your application for your data. A key advantage to using a type definition is that if you need to edit your type, every instance in your application is automatically updated as well. To add inheritance, encapsulation, and methods to act on that data, you can add class functionality to your G Type.

Within LabVIEW NXG 2.0, you can get started with shipping examples found in the Lobby  at Learning»Examples»Programming Basics»Object-Oriented Programming.

Adding Class Functionality

After creating a G Type, you can select Add class functionality from the Configuration pane, which encapsulates the G Type’s data and adds the ability for inheritance.

 

Encapsulation hides the data contents on controls, indicators, and constants of the G Type and adds support for nonpublic methods. The data can only be directly accessed within the class itself or through methods the class has made available to outside callers.

Regarding inheritance, you can select any class in your project to be the parent, and all classes must have a parent. The highest level class is “G object.” You can also select Override requirements transfer to descendants to require the override member VI’s settings to be passed to children of the class.

Removing class functionality is unsupported in LabVIEW NXG 2.0.

Adding Methods and Data Accessors

In the G Type document, navigate to the Member List tab and click the + button to choose what type of member you would like to add to your class, including VIs from static or dynamic dispatch templates. Only class members can access a class’s private data.

To select access scope and override requirements for each member function, navigate to the Item tab in the Configuration pane.

VIs for data member access can have read and/or write access and dynamic or static dispatch call behavior. You can make them directly available through Property Nodes as well.

 

 

Learn More About the Roadmap for Classes and Advanced Feature Support

NI will continue investing in intermediate-to-advanced programming concepts, like the object-oriented programming experience, in future product releases. LabVIEW NXG 2.0 adds class functionality intended for simple class-based applications. We realize in the 2.0 release there is not yet support for Actor Framework nor is there an integrated Class Hierarchy Viewer—both are examples of capabilities desirable for many applications.

Our vision is that you will be able to have a G Type with varying capabilities ranging from members and properties to access scope, encapsulation, and inheritance, depending on what you need for a particular type. Defining and managing the relationship of a data structure and the methods allowed to act on that data is a common goal, and we’ll implement the intermediary steps over time.

 

To try the latest state of object-oriented programming in LabVIEW NXG, join the NI Software Technology Preview and provide your feedback.