LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to access class data the correct way

I am moving away from the bundle/typedef style of coding towards the class style of coding. One thing I cant get past is the number of accessor VIs that are needed for access to the data in a class. I must be doing something wrong. In the old days I would take all sorts of data, put it in a bundle and make it a typedef, works great. If I wanted data all I needed to do was un-bundle the data I want and off I go. Now I'm using classes and setting all the values of the class at once is hard. In the past I would make a terminal on my front panel and pass the bundle, now how do I pass all the data to a VI in the appropriate class to write all values to the class? Do I really need to make an accessor for each item in the class? I want to just put a bundle in the class so I can have an accessor that gets and sets all the data? But this is "bad" because then I cant add new items to the bundle without breaking the class. What is best way to do this? too many VIs.jpg

 

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
0 Kudos
Message 1 of 11
(6,321 Views)

I would not see classes as a substitute for clusters - sometimes each has their own advantages.  For classes, protecting/encapsulating data and polymorphism are two of the biggest advantages. Clusters are very simple.

 

If you want to initialize class data, you can create a VI for this purpose and supply the data as inputs. Inside this VI (which is a member VI of the class), you can then use the Bundle By Name node just as you would for a cluster, so in this case it is not very different to using a cluster.  Accessor VIs are for setting individual (or perhaps more than one) class member data values.  Really they are methods since you can do what ever you want inside of them, not just set the value.

0 Kudos
Message 2 of 11
(6,265 Views)

@pauldavey

Thanks for the insight. I get what you are saying about initializing a class with a VI that is a member of the class. If you have lets say 20 settings that you need to pass from one class to another class, I wish you could make those classes "friends" so you could make a VI that is a member of both classes in order to pass the data by un-bundling one class are re-bundling into the next class data. The way it is now at some point you will either need all the class accessors, a bundle/typedef with all the data, or some ridiculous VI with 20 connector terminals. 

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
0 Kudos
Message 3 of 11
(6,199 Views)

If you have a use case where you need to pass 20 pieces of data from one class to the other, you should ask a few questions of your code.  Why do two classes need access to the same set of private data?  Are both classes manipulating that data, or is one generating/manipulating it and the other just consuming/reading it?  Should these separate classes be the same class?  Should one class be contained in the private data of the other?

 

This sounds less like a limitation of classes and more like a chance to examine your class organization and potentially rearchitect.

Message 4 of 11
(6,189 Views)

If I understand what AQ wrote, accessors are the way to go because it allows for over-riding in the children.

 

But if do not intend to go all OOP you can use libraries as I wrote about here.

 

You get most of the benefits with libraries save dynamic dispatching not being available. It is dynamic dispatching that brings all of the rules a restrictions.

 

Libraries ( the poor man's LVOOP) give you many advantages but few rules and restrictions.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 5 of 11
(6,186 Views)

@Jay14159265 wrote:

 

I want to just put a bundle in the class so I can have an accessor that gets and sets all the data? But this is "bad" because then I cant add new items to the bundle without breaking the class. What is best way to do this? 


You can put clusters in classes and add/remove items without breaking VIs as long as you typedef that cluster and update the typdef (which will push changes to the class). It also looks like you enabled access through property nodes so you can just get/set properties using that node which feels pretty similar to the bundle/unbundle.

 

You should always ask yourself whether it is necessary to make all of this data public but, in the end, you will still likely end up with a lot of VIs. It's annoying to have that many files but, from a development standpoint, if you enable access through property nodes and let LabVIEW script all of your accessors it doesn't really get in the way all that much.

Matt J | National Instruments | CLA
0 Kudos
Message 6 of 11
(6,179 Views)

@croohcifer wrote:

If you have a use case where you need to pass 20 pieces of data from one class to the other, you should ask a few questions of your code.  Why do two classes need access to the same set of private data?  Are both classes manipulating that data, or is one generating/manipulating it and the other just consuming/reading it?  Should these separate classes be the same class?  Should one class be contained in the private data of the other?

 

This sounds less like a limitation of classes and more like a chance to examine your class organization and potentially rearchitect.


Yep - if you find yourself needing to create accessors for every element of private class data, chances are good you need to rethink your design.

0 Kudos
Message 7 of 11
(6,162 Views)

1) Using bundle for these kind of tasks is bad. Bundle by name is much better. 

2) Class VIs access its data through (un)bundle by name as previously. 

3) You can greatly reduce number of data access VIs. You are working with object - think in terms of methods instead.

"Initialize" with cluster(s) of configuration parameters as input: frequency range, Instrument location. Like half of the total number do not need separate accessors any more. This vi has access to class data, so it bundles them in.

"Measure" method with Excite Volt as input parameter, result is output. 

"Calculate or get noise" - gives you S/N, other noise calculated results

4) Only a small portion of parameters needs to be directly modified outside of the class. Yes, for read/write access each needs 2 VIs.

5) If you need to modify almost all parameters outside class, it is not a class. Encapsulation is one of the things that makes class a class.

6) I think that OOP implementation in LabVIEW is limited and not very convenient compared to other languages, but it still makes many tasks (not all of them) easier.

0 Kudos
Message 8 of 11
(6,154 Views)

Hello croohcifer, I am trying to learn the best thing to do here and you have a great point. Why do two classes need access to private data, maybe that is where my mistake is. Lets say I don't want this data to be private per-se. Would that dictate that I should not convert a typedef to a class? 

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
0 Kudos
Message 9 of 11
(6,134 Views)

Do both classes need access to all of the data?

Do all of your variables belong together?

I would start by thinking about what classes make sense for your project, rather than by trying to gather an existing cluster into classes (fresh start!).  It is ok to have some parameters copied to each class.  Just think about what each class needs. What is represented by each class?

 

0 Kudos
Message 10 of 11
(6,086 Views)