I have been trying to get a handle on LVOOP for many months now but have been battling to get the concept into my head. I understand the concept of OOP, or at least I think I do, but really battling to convert that into the LabVIEW world.
Here is a list of resources that I used to try and figure it out:
- LabVIEW Object-Orientated Programming
- NI LabVIEW Virtual User Group: Advanced Object-Oriented Programming
- LabVIEW Object-Oriented Programming: The Decisions Behind the Design
- Intro to LVOOP – Part 1
- OOP 1 Hour Hands On
There have been other articles that I have used, but none that I could find started from a blank project to build a basic application.
So I set about to try write one from scratch. I used the example from the video of a Mammal Parent class with a few Child classes. Here is the UML diagram of what I had planned. I found a great online tool to create the UML diagrams. Have a look here.
As can be seen from the above diagram, I have a project with five classes, each has Dynamically Dispatched vi’s and one has vi’s for Data Member Access.
So that is the idea of my simple program, here is a step-by-step list of what I did:
Create a blank project and add five virtual folders. Add folders with the same names to the project directory on disk.
Create main.vi which will be your application.
Create the Mammal.lvclass inside the Mammal folder.
Save the class in the Mammel folder on the disk.
Next you need to create the methods for Move() and Speak() using the Dynamic Dispatch Template.
Add a blank string indicator and wire it to the connector pane. Do this for Move.vi and Speak.vi.
You have now created the Parent class with two methods that can be overridden by the Child classes.
The next step is to create the Dog, Cat and Human classes that will inherit from Mammal class and the Baby Human class that will inherit from the Human class.
Dog, Cat, Human classes:
Create the new classes and leave the control blank. Save the classes to their folders that have been created on the disk. Then edit the Inheritance settings in the class properties dialog. Each class needs to inherit from Mammal.lvclass.
Create a ‘VI for Override’ vi for Move and Speak. Edit the block diagram for each to look like this. In total three extra Speak and three extra Move vi’s should be created. One each for the Dog, Cat and Human classes.
The string constant in the No Error case gets changed for each method. This is the string that will be displayed in the main application.
Baby Human class:
Create a new class called Baby Human.lvclass and set it to inherit from Human.lvclass.
Open the ‘Baby Human.ctl’ control and add two strings to the cluster. Name them Movement and Voice. By adding controls here, we will be able to change the string that gets displayed at runtime. (Note that the Movement and Voice was hard coded in Dog, Cat and Human)
Next you need to make four vi’s for Data Member Access. These vi’s will be public and used to read and write the private data.
Select ‘Read and Write’ for Movement and Voice. Edit the icon for each if you want and then save the created vi’s.
Next you need to create the vi’s that override the Human Move.vi and Speak.vi. Follow the steps above to create ‘VI for Override’. They should look like this.
All of the above steps should set up the classes that are needed to write main.vi. I am going to write a simple application which creates an object of each class, builds an array and then displays each Move and Speak value in a For Loop. When we add the Baby Human class, we need to Set the values for Movement and Voice before we bundle the array.
For each object that gets passed into the For Loop, the associated Move and Speak method will be dynamically called.
If you change the Movement and Voice of the Baby Human class on the front panel, that change will be displayed when the application is run.
After going through this exercise I think I am starting to get a better understanding of LVOOP. I have a very long way still to go, but I now feel that I am at least on the pathway. I hope this has made sense and is able to help someone getting started.
If I made mistakes, please leave a comment so that I can correct it. You can get a copy of my project here.