If you are testing if an instance of FB_Dog is a subclass of FB_Animal I believe you could define a POINTER TO FB_Animal and use __Querypointer. I have not tested this
Thanks for replying. I am confused with your second example, shouldn't it be the other way around like in the first example? My understanding is that it is allowed to point to a child class where a parent type is expected but not the other way around?
I think your example is of no use to me, because in my case the pointer to be checked is not known. More specifically, the reference to be checked (which is initally defined as a reference to a base class) can be referenced to any child class at the implementation and the compiler will be happy about it:
VAR
rReferenceToBeChecked REFERENCE TO CLS_BaseClass
END_VAR
IMPLEMENTATION CASE 1
rReferenceToBeChecked REF=clsInstanceOfBaseClass
IMPLEMENTATION CASE 2
rReferenceToBeChecked REF=clsInstanceOfChildClass
At this point what I want is to ensure that the developer has supplied a child class (case 2) and generate an error if otherwise. That is why my intention is to confirm whether rReferenceToBeChecked is a subclass of a CLS_BaseClass.
Thank you in advance,
Omar
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi everyone,
Is there a function or way to check whether a class instance is a subclass of a class? Sorry for the alliteration
Something similar to issubclass() in Phyton.
Thank you in advance,
Omar
As far as I know there is no function as easy as issubclass().
https://help.codesys.com/webapp/_cds_operator_querypointer;product=codesys;version=3.5.13.0
Using querypointer you have to know which sublass you are looking for.
Your base class must implement __SYSTEM.IQueryInterface
I put together the following test of this function a while a go in e!cockpit:
First defined FB_animal, FB_Cat, and FB_Dog
I was then able to test like this:
If you are testing if an instance of FB_Dog is a subclass of FB_Animal I believe you could define a POINTER TO FB_Animal and use __Querypointer. I have not tested this
sorry I submitted this by accident.
Dear alb123,
Thanks for replying. I am confused with your second example, shouldn't it be the other way around like in the first example? My understanding is that it is allowed to point to a child class where a parent type is expected but not the other way around?
I think your example is of no use to me, because in my case the pointer to be checked is not known. More specifically, the reference to be checked (which is initally defined as a reference to a base class) can be referenced to any child class at the implementation and the compiler will be happy about it:
VAR
rReferenceToBeChecked REFERENCE TO CLS_BaseClass
END_VAR
IMPLEMENTATION CASE 1
rReferenceToBeChecked REF=clsInstanceOfBaseClass
IMPLEMENTATION CASE 2
rReferenceToBeChecked REF=clsInstanceOfChildClass
At this point what I want is to ensure that the developer has supplied a child class (case 2) and generate an error if otherwise. That is why my intention is to confirm whether rReferenceToBeChecked is a subclass of a CLS_BaseClass.
Thank you in advance,
Omar
Hi there,
I changed the approach so that I don't have this issue anymore.
Regards,
Omar