Method Overloading

Sometimes we need to change the standard behaviour of form controls. It is not a big deal when when it concerns overriding form controls' methods in design time in AOT. But when it comes to doing that dynamically it may not be as simple as a piece of cake.

From the beginning the goal was to make sorting functionality on the grid created by SysTableLookup class impossible for the user. It might have been a good solution to change the code like the following

formRun         = classfactory.formRunClass(args);
form            = formRun.form();
// allow the class to react on the form's events
formRun.controlMethodOverload(true);
formRun.controlMethodOverloadObject(this);
// here override Sorting on all datafields added on the grid
this.overrideSortMethodOnDatafields(form);
//<--
formRun.init();


but this class creates Datafields on the grid and names for these FormBuildControls are assigned by Axapta in an unpredictable way. So it was not possible to create methods likeFormNameControlName_Sort().

formBuildControl = _formBuildGridControl.addDataField(_formBuildDataSource.id(), fieldId);


There is another way to do that. With the big help of AxForum I created the class SysTableLookupWithoutSort which extends the system class with the new key method:

protected void turnSortingOff(Form _form)
{
 int                             i;
 TreeNode                        gridNode, controlNode, methodsNode;
 MemberFunction                  newMethod;
 str                             source = 'public int sort(SortOrder _sortDirection){; return 0;}';
 TreeNodeIterator                iterator;
 ;
 gridNode = _form.AOTfindChild('Designs');
 gridNode = gridNode.AOTfindChild('Design');
 gridNode = gridNode.AOTfirstChild();
 iterator = gridNode.AOTiterator();
 controlNode = iterator.next();
 //take the first datafield
 controlNode = iterator.next();
 this.setCompilerWarningsOff();
 for (i = 1; i <= conlen(lookupItems); i++)
 {
         //override sort method
         methodsNode = controlNode.AOTfindChild('Methods');
         newMethod   = methodsNode.AOTadd('sort');
         newMethod.AOTsetSource(source, false);
         //compile the method
         newMethod.AOTcompile();
         controlNode = iterator.next();
 }
 this.setCompilerWarningsOn();
}
Just to show how this class works in comparison with the standard class (without the sorting functionality) I also addedDemoMethodOverloadingClass class.

Moreover this class shows how various FormControls can be added on a form from scratch and their methods can be overridden from code as well: ButtonStringEditDatafields.

You can import this DemoMethodOverloadingClass project to make your experiments with dynamic programming.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.