Sunday, December 21, 2008

Determining if an object is a subclass

It can sometimes be useful to determine if an object is a instance of a subclass of some specified parent class.
While over-use of this probably suggests a class framework which has not been well designed, it is valuable in specific instances.
There are two static methods on the SysDictClass which can be used for this purpose:

static boolean isSuperclass(classId  _id, classId  _potentialAncestorId)

This method checks if the classId passed in _id is a subclass of the _potentialAncestorId
static boolean isEqualOrSuperclass(classId  _id, classId  _potentialAncestorId)
This method checks if the classId passed in _id is a subclass of the _potentialAncestorId, and will also return true if _id equals _potentialAncestorId

The following code uses the SalesTableType heirarchy as an example.
SalesTableType          stt;
SalesTableType_Journal  stt_j;
;
 
// This is true as stt is an ''instance'' of SalesTableType
if (SysDictClass::isEqualOrSuperclass(classidget(stt), classnum(SalesTableType)))
{
    // Code here will be run
}
 
// This is true as stt_j is a ''subclass'' of SalesTableType
if (SysDictClass::isEqualOrSuperclass(classidget(stt_j), classnum(SalesTableType)))
{
    // Code here will be run
}
 
// This is true as stt_j is an ''instance'' of SalesTableType_Journal
if (SysDictClass::isEqualOrSuperclass(classidget(stt_j), classnum(SalesTableType_Journal)))
{
    // Code here will be run
}
 
// This is FALSE as stt is NOT an instance or subclass of SalesTableType_Journal
if (SysDictClass::isEqualOrSuperclass(classidget(stt), classnum(SalesTableType_Journal)))
{
    // Code here will NOT be run
}

No comments:

How to identify the user that was used to change an object from AOT in AX2012

Get the object name for which we need to track these (user and date&time) information's. Login to SQL Server Management Studio an...