2

There is a problem when u define an enum in a method.

I was trying to do this:

VAR
    enumA:(A,B,C);
END_VAR

and there is the compiler reaction when I used this in TwinCAT3 Shell (TcXaeShell). enter image description here

any help would be appreciated.

asys
  • 667
  • 5
  • 20
  • What comes to mind is using `VAR_INST` instead of `VAR` in the declaration part. Unfortunately I'm unable to verify if it works, as I don't have access to PC with TwinCAT at the moment. – Jacek Domański Nov 27 '21 at 13:51
  • It doesn't work with VAR_INST either. – Jakob Nov 27 '21 at 13:52

3 Answers3

5

You can only use global enumerations in methods. It's one of the limitations with local enumerations.

https://alltwincat.com/2021/11/16/local-enumerations/

Jakob
  • 1,288
  • 7
  • 13
2

You should first define variable type as enumeration in DUT

TYPE MyEnum:
    (A, B, C)
END_TYPE

Then in a program you can declare variable of that type

VAR
    enum: MyEnum;
END_VAR

Inside the program if you want to compare it.

IF enum = MyEnum.C THEN
    // Do something
END_IF;
Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38
  • 1
    The question was meant to be "How to define *local* enums in methods", so @Jakob gave the more correct answer: "You can't". Though this might still help someone new if they don't know how to define global enums. – Guiorgy Nov 29 '21 at 12:09
  • "In a method" is a ritorical part of the question. He asked "how to define enum in a method" rather than "How to define enum" not because he knew how to define enum globally. By the nature of his question it is obvious that he does not know how to define them at all. So my unswer is direct to this question from my perspective of view. – Sergey Romanov Nov 29 '21 at 14:03
  • 1
    @SergeyRomanov tnx for taking your time on this question and sorry for misunderstanding the question, I can ask my question in a more specific way by this "How can define enumeration `locally` in Methods?", by the way, your answer may help anyone who looking for a solution for this problem so this is good to keep this answer too. – asys Nov 30 '21 at 04:27
2

I’ve run into this issue before. You must declare the local enumeration in the variables section of the function block. Then you can use it in the methods of the function block.