LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to manage two classes with similar methods

The problem is the following:

I have a program that performs a set of actions on a class.
The methods of the class are very abstract and high level.

I need to be able to switch between this class and another class that would have the exact same set of methods from the outside (the VIs would have the same names, inputs and outputs - the internals would be different of course).

What is the usual way to do this ?


I am not very familiar with object-oriented programming, so my question may be quite naive. Would this be typically solved using inheritance ?
Or should I have only one class with internal case selectors in the methods, that would cause different actions to be performed depending on an internal switch ?

Hypothetical example to illustrate my question:

Let's say that I have a program named "Travel" that calculates travel patterns in a population.
It uses a class named "Means of transportation" whose methods will return the cost of an itinerary, the CO2 emitted, the time of travel, etc.
I want to be able to run the program with both airplanes and trains (separately, these will be distinct use cases).
The "airplane" and the "train" classes have the exact same methods from the outside (and I collectively call them "Means of transportation").
How can I switch between the "airplane" class and the "train" class without having to replace by hand all the instances of the class and its methods in my program ?
Do I need to have a more generic "means of transportation" class with internal selectors in the methods that act differently according to whether the interal switch in the class data is set to "airplane" or to "train" ?

I'm sure that this is a very typical problem in OOP, but I'm specifically interested in how this can be handled nicely and neatly in Labview.

0 Kudos
Message 1 of 10
(3,015 Views)

maybe this will help

LVOOP

 

best regards

Christian

0 Kudos
Message 2 of 10
(3,009 Views)

Hi Calvintiger,

 

I am quite new to LVOOP too so I'm sure some of the gurus on the forum can follow up quite soon with a more complete reply! Howvever I did something similar to what you are asking quite recently so here goes:

 

In a simple example what you do is you create your 'Means of Transportation' class and two classes to inherit from this - Airplane and Train. All three classes should be in the same project. You can set the inheritance by right-clicking on the class and modifying it there.

 

When you make a subVI in ''means of transportation' parent class make one in the other two child classes with exactly the same name - for example 'calculate.vi'. When you create these three subVIs on the connector pane right-click on the object and select 'dynamic dispatching'.

 

At the start of your code have a case selector whereby you can chose between Airplane and Train. From there on in if a subVI only exists in 'Means of Transportation' that will be used - however if a VI exists in the child class that one will be used as opposed to the one in the parent class.

 

I hope this helps - there is also a reasonably good example with the LV help if you search for Dynamic Dispatching,

 

All the best,

Dave

 

 

0 Kudos
Message 3 of 10
(3,007 Views)

Hello Dave,

 

Thank you very much for these tips, they seem like a good starting point.

Just one question: according to you, where would the case selectors be present in the code ? (I mean, where would the case be checked ?).
Furthermore, why is dynamic dispatching necessary ? Wouldn't this prevent me from copying the contents of the class ? (multiple instances of this class might exist).

0 Kudos
Message 4 of 10
(3,005 Views)

Hi Calvintiger,

 

In my I am programming two versions of a camera. So I have 'camera' parent class and two child classes - one for each model. Initially I load whichever camera type is connected from an .ini file (or a UI popup). I wire this to a case structure which contains an object constant - case 1 is camera-type#1 constant and case two is camera-type#2 constant. I  program by reference so I create a DVR to the 'camera class' and then a DVR to 'camera#1 class' within case 1 and so on.

 

As long as dynamic dispatch is selected for inputs and outputs the code will select the subVI for the relevant camera or for the parent class.I know this is a relativly simple application of the concept but it works for me!

 

I'm afraid as a relative beginner I don't have any idea how to answer yoru second question - sorry about that! Like I said hopefully one of the guru's can jump in soon;-)

 

Regards,

Dave

 

 

 

 

0 Kudos
Message 5 of 10
(2,998 Views)

Thanks for the extra tips 🙂

 

Two remarks:

-It seems that it is not required that the two classes belong to a project. I was able to select the inheritance relationship between the two opened classes without creating any project.

-How is it possible to have to VIs (from two different classes) with exactly the same name ? Since the files have to be saved on disk with that name, won't there be a conflict ?

0 Kudos
Message 6 of 10
(2,992 Views)

I agree regarding the two classes not being in the same project - its something I hadn't tried so I assumed they needed to be in the same project....so I've just learned something new;-)

 

As for the .vi having the same name as long as they are in different classes this is allowed.....

 

0 Kudos
Message 7 of 10
(2,985 Views)

I just re-read you post - the files can just be saved in different folders. Usually I use the structure \project\class1\methods\method1.vi and project\class2\methods\method1.vi

 

 

0 Kudos
Message 8 of 10
(2,982 Views)

Err, but in this case the VI files need to be saved in different locations, otherwise they will overwrite each other..

0 Kudos
Message 9 of 10
(2,981 Views)

\project\class1\methods\method1.vi

\project\class2\methods\method1.vi

 

Maybe we have crossed wires (excuse the LabVIEW pun;-)).....as I said above if the subVIs are saved in different folders on the hard-drive they won't over-write each other.....

0 Kudos
Message 10 of 10
(2,975 Views)