LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Instantiate mulitple child classes?

All,

I've been trying to research this for a while now and have not found any threads that discuss it

yet.  I'm expanding the hardware classes in my application.  This is what I'm planning for the

future:

Parent = Hardware
Child = AI, AO, and switching
Grand children = the individual devices for each child

 

Currently, I only have one HW type (AO child) and multiple devices of that type (grand children)

which are dynamic dispatched from the child.  That is all fine and working, and I have a pretty

good grasp on the concept there.

 

But I want to know (if it is possible first) how to implement the classes so I can have one

hardware class to pass between states/sub VIs where I can access all the children and

grandchildren down stream.

 

Conceptually, my problem lies in fact that I could have 1-3 devices being utilized at the same

time.  How do I instantiate the second and third child classes on the parent, so that downstream

the grandchildren know how to act?  Dynamic dispatch is not applicable between the parent and

child classes.

 

I've seen some posts that suggest a DVR to hold class data, but the class control errors if you

include a child/grandchild class in it.  I can break out the individual class data items and hold

them in the DVR, but I don't see how that helps solve my problem.  I've played around with a few

other ideas but nothing is jumping out as I'm headed in the right direction, and I don't have

anything in a state where I can post it at the moment.

 

So I guess the question boils down to this:  How can I instantiate (up to) 3 different child

classes on a parent class wire simultaneously?

0 Kudos
Message 1 of 6
(3,151 Views)

You make a 3-element array. One is Grandchild 1, One is Grandchild 2 and the other is Grandchild 3.

 

You handle them as if they are Parents (Array type will "SEEM" to be of type Parent (or Child if applicable) but it will in reality be the correct type).

 

Don't be fooled by the wire colour or what LV says int he IDE is the datatype of the wire.  It simply tells you what the common datatype of all array elements are, each array element retains its true identity and will still execute the correct method downstream.

0 Kudos
Message 2 of 6
(3,133 Views)

@AMP12 wrote:

So I guess the question boils down to this:  How can I instantiate (up to) 3 different child

classes on a parent class wire simultaneously?


I think you have a conceptual misunderstanding here. This can probably be boiled down to the difference between "is a" and "has a". A pickup truck is a type of vehicle. A household has two vehicles, one of which is a pickup truck. In this example, you have 3 classes, where pickup is a child of vehicle (that's the "is a" part). Household is a completely separate class which contains an array of vehicles in its private data. It sounds like you're missing the equivalent class (Device?) in your model. You should read up on OOP to become familiar with concepts such as compisition if you plan to use it.

 

Also note three things:

 

  1. DD can only be used for actions which are actually similar, because the connector panes have to be the same. That means you can't have AO:Write and DO:Write, because one takes analog values and the other takes digital ones. There are various ways of working around this, but it's something to be aware of.
  2. Having an additional class which is a container might require you have to have the same VIs as wrappers in the container class as well to avoid having to extract the HW class each time to the outer layer.
  3. LV is by value by default. This can be sometimes be an issue, depending on how your code is written and how the hardware works. That might be the source of the DVR advice you got, which is probably meant to ensure that there's a single canonical object which everyone is referring to. You might not need this.

___________________
Try to take over the world!
0 Kudos
Message 3 of 6
(3,045 Views)

Intaris,

 

But my class control returns an error anytime I include any child classes in it, whether in a cluster (which I tried origianally) or in an array.

 

Or are you saying to run a separate array wire around the BD for the child classes, in addition to the parent class wire?

0 Kudos
Message 4 of 6
(3,003 Views)

tst,

 

The concept of composition is something that I had been missing, so thanks for introducing me and keying me into the right terminology.  That said, I've had no luck finding an real world examples of composition.  Everything I seem to find is based on inheritance, and not composition.

 

Reading back over my original post I see I may have mixed words slightly and jump between my current app and future app.  Using the example you proposed, I'll elaborate on only my future application, and be sure to watch my wording.

 

Parent class = Household (only 1 household)

Child classes = vehicle Need (hauls heavy cargo, hauls people, goes fast)

Grandchild classes = vehicle Type (1/2 ton truck, 3/4 ton truck, cargo van, 7pass SUV, 12 pass Van, station wagon, Corvette, Camaro, porsche 911)

 

I only have 1 Household.  The 1  "has a" bunch of vehicle Needs.  Each vehicle Type "is a" X, depending on the vehicle Need situation.

 

I have the inheritance parts of OOP between the dynamic dispatch of a vehicle Need to the vehicle Type down and well understood.  When I need to haul stuff, I use the trucks or cargo van.  There is never a need to haul stuff and go fast in the same Need/Type, as they are two different, unrelated Needs.

 

But between the parent and child classes, I do have a need to haul heavy cargo, and a separate need to move people at the same time.  Each vehicle Need will be executed by different vehicle Types, but I do I need to haul the land scape rocks at the same time little Timmy's soccer team needs to go to practice.  It isn't one or the other, it is both.  (And later add the case where my wife needs to drive home fast, so all 3 needs spontaneously)  How to I put that information back into the household class?

Is there any technique for doing this that follows the OOP principles?

 

The only way I can see it, I would need to create an array to hold each implementation of each child and grand child class, then re-instantiate it each time it is needed.  I'm conceptually missing how the data for vehicle need and vehicle type are saved between uses.  The only way I can see to do it is put every piece of child and grandchild data into the parent class control, then use an array to hold each.  Is this right?

 

Per your notes...

1.  Got it, as discussed above.

2.  Can you elaborate what you mean here?

3.  I understand this if it is referring to the individual data itself as a reference.  But if it is the class, I don't see how to use the class wire itself in the DVR.  My class control gives me errors every way I've tried so far.

0 Kudos
Message 5 of 6
(2,988 Views)

I think you still have a big misunderstanding. Inheritance defines functionality using the "is a" principle. A truck can't be a grandchild of household, because it doesn't satisfy that relationship. The household here is an unrelated class which holds objects inside it representing the vehicles it has.

 

Also, you don't have a class (or even an object) executing. VIs are the things which execute. The VIs can be part of a class and they can act on an object and change it. This can be done in parallel or not, depending on the code.

 

I think you need to better understand the concepts of OO and also to better define what you want to do. For instance, you talk about having an AI class and then having children of that. Usually, the reason for having something like that is that you have different devices which can perform the same operation (read AI), but they each perform it in a different way (for instance, one uses DAQmx, one uses Modbus, etc.) and you want to be able to interact with all those devices using the same code, so you define the read AI VI as DD and then override it. I get the impression that this isn't the case with you.

 

Like I said, I woudl suggest doing some reading on OO and then clarifying to yourself exactly what it is that you want (forget the OO terminology. Just understand what functionality you want).


___________________
Try to take over the world!
0 Kudos
Message 6 of 6
(2,951 Views)