09-11-2019 09:57 AM
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?
Solved! Go to Solution.
09-11-2019 10:29 AM
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.
09-11-2019 10:51 AM
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?
09-11-2019 10:56 AM
@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.
09-11-2019 10:58 AM - edited 09-11-2019 10:59 AM
@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?
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.
09-11-2019 11:03 AM - edited 09-11-2019 11:04 AM
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?
09-11-2019 11:08 AM
09-11-2019 11:09 AM
@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.
09-11-2019 11:13 AM - edited 09-11-2019 11:14 AM
@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!
09-11-2019 11:43 AM
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 🙂