08-13-2010 07:14 AM - edited 08-13-2010 07:15 AM
Hi everybody,
I am just doing my first steps in object oriented programming in Labview (did some C++ before..). I try to write a program to handle the data acquisition of several DAQ-Devices (Camera, Scanner, AD-Cards and so on). I decided to write a class (DAQ-Dev) which implements all common functions, like Record, Init or Close. So I created these VIs as dynamic dispatch VIs and overwrote them in the specific Sub-Classes (in camera.lvclass, adcard.lvclass and so on). Now, I can, by passing the specific DAQ-Device to my Record.vi, start the data aquisiton.
But now the Problem: There are several functions that AREN'T part of every class, like for example "Set Region of Interest" for the Camera or "Set Channels to Acquire" for AD-Card. Thus, I have these member-functions in my classes, but they can't be executed with an object of type "DAQ-Dev" as they aren't part of this parent-class. So I wrote a part of code to convert my classes to more specific ones - but as I'm doing this the first time, I just wanted to ask if there are better / more elegant methods to do this. Attached, you see part of my code looking up the specific class and then converting it to the object type needed.
Thanks for your opinions and help,
Christian
08-13-2010 07:27 AM - edited 08-13-2010 07:28 AM
You're better off trying to cast the class type to Camera and seeing if an error occurs (the bit outside the loop). This is because you could have children of "Camera.lvclass" which would support the operations you want but won't be called "camera.lvclass".
The primitive you need called "To More Specific Class Function" and is found on the Cluster, Class and Variant palette.
On a different note. It's generally better to seperate out such incompatible classes in your code so that the stuff you need to work with as "camera" has "camera" as the lowest common denominator. This avoids all of the "Is this class this type" which is kind of defeating the purpose of LVOOP.
Shane
08-13-2010 07:35 AM
I am not sure if I understand your answer correctly...let me just point out that my parent class is called "DAQ-Dev.lvclass" (which the array on the left is type of), my child class is Camera.lvclass (which has no more childs). I already used "To more specific class", as I cast "DAQ-Dev.lvlcass" to "Camera.lvclass"...if I separate Camera from DAQ-Dev, as I understand your solution, I would loose the possibilty to call a common Record.vi for either Camera, AD-Card (which is also child of DAQ-Dev) and so on, which was my main intention in programming it this way...
Do I understand you correctly that you wouldn't inherit Camera from DaqDev?
Thank you,
Christian
08-13-2010 08:23 AM - edited 08-13-2010 08:25 AM
No I'm not suggesting you don't inherit. I have no way of knowing you're using common methods from the picture you posted. You stated you're starting out with LVOOP so I thought your set-up might be less than optimal.
What I'm saying is that in this case if you cannot keep the objects seperate your search routine should simply try the upcast and observe the error output. If the object cannot be cast up, the error case is true. You might have no child classes at the moment, but trust me, it pays to keep the option open now. Using the class name as a way of selecting the valid objects is IMHO not the best way to do this.
So take the part currently outside the loop and put it IN the loop. Use the error output to determine if the object can be upcast to "camera".
Shane.
PS It might be useful to write a sub-VI (general purpose) to extract all elements of an array which correctly can be upcast to a specific class (with both object array to search and the target class being on teh connector pane of the Sub-VI and using the method I have listed). This is a common issue with LVOOP.