LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Class methods - Valdating data / calling

Everyone here helped me clear up some serious confusion with my use of accessors and some design tips; now I am back with a follow up question.

 

In my situation I have written a class called "calculations" which contains a control with 18 objects. I wrote a method for each required calculation; some methods used other methods to complete the calculation as they are dependent on other calculations happening prior. These methods are all private.

 

My problem is understanding how to populate the class control with the data required to complete the calculations. Currently my methods don't validate the data contained in the class control as I'm assuming it will be present when the method is called.

 

If I want to make a calculation I envision a public method that I must pass the required data to complete the calculation; this method will validate that data, populate the correct objects in the class control, and call the correct private methods to complete the calculation. It will than get the completed calculation from the class control which will be the output of this method. (like a function in .NET; just a single output)

 

Is this the correct approach? Doing it this way seems to allow me to add additional calculations without much worry about screwing up existing calculations.

 

Sorry for the long winded post......still learning here 🙂

 

-Chris

0 Kudos
Message 1 of 5
(2,862 Views)

Hi F1_Fan,

 

I'm a little confused. You want to do calculations on private member data using private member functions, but have a public member function as an accessor that will also write the data to the private member data locations? Why would the data not already be in the class?

 

Generally you would have separate accessor functions to first write data to the class instance, then a simple public function do something on it (which would use an accessor) - but I could be wrong.

 

Maybe you can explain your application on a higher level so we know what you're trying to do?

Jeff | LabVIEW Software Engineer
0 Kudos
Message 2 of 5
(2,837 Views)

F1_Fan wrote:

Everyone here helped me clear up some serious confusion with my use of accessors and some design tips; now I am back with a follow up question.

 

In my situation I have written a class called "calculations" which contains a control with 18 objects. I wrote a method for each required calculation; some methods used other methods to complete the calculation as they are dependent on other calculations happening prior. These methods are all private.

 

My problem is understanding how to populate the class control with the data required to complete the calculations. Currently my methods don't validate the data contained in the class control as I'm assuming it will be present when the method is called.

 

If I want to make a calculation I envision a public method that I must pass the required data to complete the calculation; this method will validate that data, populate the correct objects in the class control, and call the correct private methods to complete the calculation. It will than get the completed calculation from the class control which will be the output of this method. (like a function in .NET; just a single output)

 

Is this the correct approach? Doing it this way seems to allow me to add additional calculations without much worry about screwing up existing calculations.

 

Sorry for the long winded post......still learning here 🙂

 

-Chris


Sounds good for the most part. Notes...

 

Private methods can only be accessed from the Class while Protected functions can be used by the decendents so you may want to use that scope setting instead of Private.

 

Before you go to far try crating a child class and make sure it will do what you have in mind. I make this suggestion becuase Dynamic Dispatched VIs must all have the same protection and icon connector. If you go to far with the parent class and latter find out you have to change something to get the child right, you will have less work to re-do OR work to figure out "what the HE#@" that error message means and which of the 100 methods in three class is casusing the problems.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 3 of 5
(2,833 Views)

Correct enough; my child class doesn't work 😞  Good news is I now know why; private scope.

 

 

I basically make several calculations; these calculations are involved and require numerous inputs. I constantly reuse the things so they are practically begging to be bundled in a nice class (as I know classes from say .NET).

 

I have written all the methods for each INDIVIDUAL calculation; my confusion is in the way to carry out these calculations. For example if we have a circle inside of a square and we want to know the total area minus the circle. The steps to solve it would be to calculate the area of the square then the area of the circle followed by subtracting the area of the circle from the square. The inputs would be length, width, and radius with output being the area. Simple, but I don't want a single method for this calculation I need to use the circle and square area calculations for other things.  

 

In my idea of how this class stuff is supposed to work I would create a class called say calculations. Inside that class I would create methods to calculate the area of the square and another for the circle. I would write accessors to populate the class control and accessors to get the result. Where would I carry out subtraction of the circle from the square???? That is where my confusion lies. Would I do it external of the class? In a child class? Or just in a typical VI that would reside inside my QSM?

 

 

 I'm almost embarrassed at all this confusion; just makes me feel like an idiot sometimes 😞

 

0 Kudos
Message 4 of 5
(2,823 Views)

Just some thoughts, not necsarily good...

 

Calc Class

data

Instance of Class Shape (background)

Instance of Class Shape (forground)

Method

Compute area

Set Length

Set Witdh

Set Shape Background

Set Shape Forgraound

 

Shape Class

Data

Area

Method

Compute Area THis is a no-op unless you want to define how to take the area of an undefined shape

 

Elipse Class (child of shape)

Data

Major axis length

Minor Axis Length

Methods

Set Length Overide of parent and saves to major Axis

Set Width Similar to Set Lenght but write minor axis

Compute area Over-rid of shapes method and computes are from major and minor axis and save as

Get Area Get the area value

 

Square Class (another child of the class Shape)

Data

Length

Width

 

 

When you want to know the net area of that non-overlapped region you would

 

Use the Shpae Class methods to define the geometry of the two shapes and save them as forground and background.

 

Clac Class "Compute Area" could be coded as ...

 

Read Background and use method "Compute Area"

Read forground and use method "Compute Area"

 

Subtract second from the first and you have your area.

 

Swithc swap the type written to forgraound vs background it should still work.

 

Use circle class for for and background should still work.

 

So if you write it once, you can very easily aapt it to do the other variations.

 

Re: Feeling stupid.

 

I still feel that way and that is why I am replying to your quesions. If I am spreading BS, then it should smell that way and hopefully someone will stomp on me for spreading the bull before I step in it. Smiley Wink

 

Ben

 

 

 

 

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 5 of 5
(2,815 Views)