This is what I would like to have (this is a constructor for FB object):
METHOD FB_init : BOOL
VAR_INPUT
bInitRetains : BOOL;
bInCopyCode : BOOL;
//My variables:
typeOfVariable : TYPE; // This obviously doesn't work
END_VAR
size := 1;
myArray := __NEW(typeOfVariable, size); // Create dynamic array with 'typeOfVariable' variables.
END_METHOD
typeOfVariable
for
example REAL
and the method would create array of REAL
variables with
size 1
.type
I declare typeOfVariable
so it can store the
data about type
of another variable.Working example is the __NEW()
method for dynamically creating array.
This method takes in a argument such as REAL
or INT
.
This is the code for it:
myArray := __NEW(REAL, 10); //Create array with type REAL variables with the size of 10
OK, here a small example how you could tackle this problem:
Create an Enum first:
TYPE E_Type :
(
eNO_TYPE := 0,
eINT,
eREAL
);
END_TYPE
Use it in a switch case:
METHOD createArray : POINTER TO BYTE
VAR_INPUT
eType : E_Type;
size : UINT;
END_VAR
CASE eType OF
eINT:
//Remember to __DELETE
createArray := __NEW(INT, size);
eREAL:
createArray := __NEW(REAL, size);
END_CASE
Check for Null-Pointer and remember to __Delete when you don't need the array anymore.
this is actually implementation I am using now. But I am still curios if there is another way to do it - therefor I put this question here in hope it can be answered :)
+1
but I won't accept it. Would you consider giving the question also+1
? It has been never asked on SO so far (in context of IEC61131-3) ... And I really hope someone can find a solution or explanation why it can't be done.Simple answer: You can't pass the Type directly, the compiler won't allow it. But you can go around it with implementations like the one above..It must be said that when developing code for industrial machines you should be aware of the special context you are in.Creating dynamic memory is used in the rarest occasions and should be avoided when not necessary. You have a very similar philosophy in the ADA language (83 or 95) widely used in military and civilian aviation.
Ah okay thank you! Didn't know that! :)