plctwincatstructured-text

Are there any cons to using VAR_INST


Ever since I started working with TcUnit I have been, in many cases, working with a lot of VAR_INST declarations for a whole bunch of tests. This is due to the nature of how PLC world works, where you need to keep calling the body to do something, the tests are running in "parallel", which could mean tests that share the same function block must be sure what state it is in - if another test would be changing any property it would fail every other test.

My question is, are there any cons to using instanced variables inside methods? This is not a real world example I usually do, I very rarely find the need to use them on machine code, but in case I do, I was wondering if I should take notice when I do use it.


Solution

  • No disadvantage to use VAR_INST. It will become an instance variable, but only accessible from the method of where it is declared.

    I rarely use VAR_INST, except in the cases of defining test-cases using TcUnit, which is a perfect example of where VAR_INST makes a lot of sense. This makes it possible to instantiate the subject-under-test (SUT) in your test-method and thus it won't have any dependency to any other tests (tests should not depend on other tests). The only disadvantage of using VAR_INST compared to declaring your SUT's as normal instance variables (VAR) is that it's not possible to inject dependencies to FBs that are declared as such. There is an excellent FAQ in the TcUnit website that discusses this and what you can do about it.