LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

OOP - Child Needs to Add On to Parent Method Functionality

Solved!
Go to solution

I have not created many classes in LabVIEW, so I wanted to see if I'm doing this wrong.

 

A while ago I created a Timer class. It has some timing info and the state of the timer, and you can check it in a loop to see if the set time has elapsed.

 

Recently I had a need for an Alarm class. I want it to behave similar to Timer, but if a boolean input is true for a certain amount of time then the alarm output goes high for 1 call of the check method. Since Alarm is similar to Timer with some extra bits of logic needed, I made it a child of Timer. Does this sound right?

 

Now I have a couple methods which are similar, but slightly different. For instance, when I Check the alarm, (depending on the alarm state) I want to check the timer (calling Timer.lvclass>>Check.vi) and modify the alarm state if it has been tripped. However, I can't name this method Check.vi because I already have that method in the parent. I can rename it Check Alarm.vi, but I feel like having to rename it may just be because of my poor design choice. Any suggestions here?



CLA // LabVIEW 2016 // BALUG // Unofficial Forum Rules and Guidelines
0 Kudos
Message 1 of 10
(377 Views)

You need to set the type of Parent method class terminals to Dynamic dispatch Input and output. Then you can create child class method that will be called, manually or New->VI for override. It should have the same name and terminals. 

If option New VI for Override in child class is grayed, there are no VIs for override available, probably parent vi has wrong settings. You can create New VI from Dynamic Dispatch template in parent class and see what settings are different.

Message 2 of 10
(350 Views)

I'm sorry, maybe I did not explain it clearly. I do not want to completely override the Timer.Check method. Because, in Alarm.Check part of the function is to check whether the timer has elapsed and modify the alarm state accordingly. If I override Timer.Check then I will not be able to call it inside Alarm.Check, correct?

 

Capture.PNG



CLA // LabVIEW 2016 // BALUG // Unofficial Forum Rules and Guidelines
0 Kudos
Message 3 of 10
(334 Views)
Solution
Accepted by topic author Gregory

@Gregory wrote:

I'm sorry, maybe I did not explain it clearly. I do not want to completely override the Timer.Check method. Because, in Alarm.Check part of the function is to check whether the timer has elapsed and modify the alarm state accordingly. If I override Timer.Check then I will not be able to call it inside Alarm.Check, correct?

 


It sounds like you're looking for the Call Parent Method function, which calls one method up the class hierarchy. I don't know where it is on the palettes, but if you open quick drop (Ctrl-Space) and type in the name it'll come up.

 

Also, in the Project Explorer, if you have a parent class setup with Dynamic Dispatch terminals, you can right click on the Child class and select New -> VI for Override, which gives you a template with Call Parent Method already inside it.

Message 4 of 10
(330 Views)
Solution
Accepted by topic author Gregory

@Gregory wrote:

I'm sorry, maybe I did not explain it clearly. I do not want to completely override the Timer.Check method. Because, in Alarm.Check part of the function is to check whether the timer has elapsed and modify the alarm state accordingly. If I override Timer.Check then I will not be able to call it inside Alarm.Check, correct?

 

Capture.PNG


The child's override method can call the parents methods, therefore simply extending the functionality and not completely replacing it. You would use the "Call Parent" method to do this.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 5 of 10
(326 Views)

Bert and Mark,

 

Ok, great, I was just playing around with the Call Parent Method when you replied. So it seems like the override method will work, but I need the exact same connector pane as the parent method. If I want Alarm.Check to have a different number of outputs than Timer.Check, then I do need to rename Alarm.Check and make it a separate method, is that correct?



CLA // LabVIEW 2016 // BALUG // Unofficial Forum Rules and Guidelines
0 Kudos
Message 6 of 10
(319 Views)
Solution
Accepted by topic author Gregory
Yes. A method override is to do the exact same thing as the parent, but in a different way. For example, a "Clear" method for a Timer might be to reset the timer to zero, but for an Alarm it might need to both reset the timer to zero and clear the Alarm state internally. If your child method "does" something differently from your parent, it shouldn't be an override. By the way, I'm not exactly clear on the distinction between an Alarm and a Timer in your case- you said you check the Timer to see if the time has elapsed, but it sounds like the Alarm just notifies you if the Timer has elapsed as well. Is that right? If the Timer is just keeping track of elapsed Time, and the Alarm indicates when a setpoint has been passed, I'd suggest not making Alarm inherit from Timer, as they're functionally different things. An Alarm "has a" timer, not "is a" timer, so that'd point to Composition. Give Alarm a Timer object in its private data that it can use as it wishes to monitor the elapsed time.
Message 7 of 10
(309 Views)

@Gregory wrote:

Bert and Mark,

 

Ok, great, I was just playing around with the Call Parent Method when you replied. So it seems like the override method will work, but I need the exact same connector pane as the parent method. If I want Alarm.Check to have a different number of outputs than Timer.Check, then I do need to rename Alarm.Check and make it a separate method, is that correct?


Yes, that would be correct. Or, you can create accessor methods that would get the data. This results in two calls in your code, but it provides a clean interface. Alternatively you can use an override method and then create a wrapper method in the child with a different connector pane.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 8 of 10
(305 Views)

@BertMcMahan wrote:
 If the Timer is just keeping track of elapsed Time, and the Alarm indicates when a setpoint has been passed, I'd suggest not making Alarm inherit from Timer, as they're functionally different things. An Alarm "has a" timer, not "is a" timer, so that'd point to Composition. Give Alarm a Timer object in its private data that it can use as it wishes to monitor the elapsed time.

Ooh OK, that makes a lot of sense! The distinction is that Timer will tell whether a time has elapsed with stopwatch functionality. Alarm tells whether the Alarm Condition boolean has been True for a certain amount of time. Going False will reset the alarm timer. So I think what you said is exactly correct, the Alarm has a Timer!



CLA // LabVIEW 2016 // BALUG // Unofficial Forum Rules and Guidelines
0 Kudos
Message 9 of 10
(302 Views)
Highlighted

I've marked Bert's message 7 the solution because it answered the question I didn't know I was asking. But I also marked the messages mentioning "call parent method" because they answered the question I was asking Smiley Happy



CLA // LabVIEW 2016 // BALUG // Unofficial Forum Rules and Guidelines
0 Kudos
Message 10 of 10
(286 Views)