11-21-2013 03:38 PM
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.
Solved! Go to Solution.
11-21-2013 04:24 PM
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.
11-22-2013 02:54 AM
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?
11-22-2013 09:32 AM
@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.....
11-22-2013 12:29 PM
@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.
@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.
11-23-2013 08:54 AM
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