LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

LVOOP - 1 Array of Parents to multiple Child Arrays?

Solved!
Go to solution

I have 3 channel types in my system. DI, AI and AO.

 

I want to create a CHANNEL Parent Class which has 3 Children for each of my channel types.  This is because I want to store (on disk using flatten to string) all of my channels together as an array of channels.  This is ok because I can use the "To more generic class" and save them as an array of my parent (CHANNEL class).

 

The difficulty comes when I read this array of objects.  I can read my array of parent objects but I then want to turn my array of channel objects into an array of DI objects, an array of AI objects and an array of AO objects.

 

Any ideas?  Thanks.

Lewis Gear CLA
LabVIEW UAV

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

Wire your array of the parent class into a for loop. Add shift registers of empty arrays of child objects, one for each child. Attempt to cast (using To More Specific Class) each parent class array element to the child types. If you don't get an error, then the object is that type of child, so add it to the appropriate array.

 

Alternatively, add a method to the parent class called Get Type, and override it in each child class, then call it on each element of the array to determine its type.

Message 2 of 6
(3,711 Views)

Hi nathand thanks for the ideas.  I have implemented it like you sugested.  Is there a way to make it more scalable?

 

The more child classes i  the messier it will get.  Even though the code is the same for both, I can't have a generic subVI because the child class is different for both.  Do you think it is a good idea to have a new array class for each child.  For example,  I could have a DO Array Class with a method that has a channel parent input and trys to add it to the aray?

 

OOP.png

Lewis Gear CLA
LabVIEW UAV

0 Kudos
Message 3 of 6
(3,688 Views)

@nathand wrote:

Wire your array of the parent class into a for loop. Add shift registers of empty arrays of child objects, one for each child. Attempt to cast (using To More Specific Class) each parent class array element to the child types. If you don't get an error, then the object is that type of child, so add it to the appropriate array.

 

Alternatively, add a method to the parent class called Get Type, and override it in each child class, then call it on each element of the array to determine its type.


And make sure the type is a string otherwise problems ensure later in life.....

0 Kudos
Message 4 of 6
(3,671 Views)
Solution
Accepted by topic author Lewis_G

@Lewis_G wrote:

Hi nathand thanks for the ideas.  I have implemented it like you sugested.  Is there a way to make it more scalable?


You could abuse the Preserve Run-Time Class primitive, as shown below. This splits the parent array into separate arrays for each child class, but the wire type of each element of those arrays is still the parent type, even though they carry children. Not sure if this does what you want, or not.

Array of Classes to Arrays of Child Types.png


@Lewis_G wrote:

The more child classes i  the messier it will get.  Even though the code is the same for both, I can't have a generic subVI because the child class is different for both.  Do you think it is a good idea to have a new array class for each child.  For example,  I could have a DO Array Class with a method that has a channel parent input and trys to add it to the aray?


This might be fine, if it meets your needs.

 


@Intaris wrote:

@nathand wrote:

Alternatively, add a method to the parent class called Get Type, and override it in each child class, then call it on each element of the array to determine its type.


And make sure the type is a string otherwise problems ensure later in life.....


I thought about mentioning that when I first suggested it, but then I thought there could be situations in which an enum would work fine if you don't mind violating good OOP practices. Yes, the parent would know about its children, and you would have to know about all the children at design time, but if you can accept those conditions then an enum is simple. I'm undecided on whether I would ever implement this solution if I were trying to solve this problem in my own code.

Message 5 of 6
(3,655 Views)

One thing I have done in the past is pass an actual class as the parameter to "retrieve" the required data sets.  Then have the classes identify themselves.  The added benefit is that you can make use of the "Preserve Run-Time" functionality and automatically have the output class of the right type.

 

Shane

Message 6 of 6
(3,638 Views)