calling form parm methods in class


i have created a form with all unbound controls(all string fields and no datasource). now i have to pass the valuesof these fields into a class. (For that i have created a menuitem button on the form and gave the class name in its properties.)

for that i have written parm methods in the form for these fields like

public str parmPrimekey(str primarykey)
primarykey = Primarykey.valuestr();
return primarykey;

Now, please tell that how am i going to call this form parm method in which class?
DO i have to write parm methods in class also?


It can be done, but you shouldn’t do that. Let me explain why.

AX forms are not types, therefore you can’t call their methods in a type-safe way. You lose compilation-time control, cross-references etc., which makes any code with such calls hard to maintain and error prone.

But you have full type safety if you call class methods from forms, so you actually want to reverse your approach. There are several possible designs to keep type safety, for example:

  • Implement parm methods in a class and call them from the form when needed (typically when starting the form and when a value has changed).
  • Pass form control references to a class and let the class to read and set values to them.
  • Use a temporary table.

If you really need to call a form method (but this should be exceptional), you need to cast the form reference to Object and call the method - compiler allows you to call any method, even if it doesn’t exist. That’s what we need here, but that’s exactly what we would like to check at compile time.

See the example:

FormRun form = ...
form.myOwnMethod(); //Compilation error
Object formAsObject = form;
formAsObject.myOwnMethod(); //OK

You may also consider to check if the method exist by using formHasMethod().

By the way, you have several things wrong in your parm method - it must be called with an argument but it only returns the current value of Primarykey.

hi Martin,

yes i used it as mentioned by you:

Object formAsObject = form;
formAsObject.myOwnMethod(); //OK

meanwhile please explain as written by you :" it must be called with an argument but it only returns the current value of Primarykey". It is returning correct string value to me. Did you mean that my method definition is incorrect?



I obviously failed in explaining why this is wrong thing to do. :frowning:

About your parm method, this is what it really does:

public str parmPrimekey()
    return Primarykey.valuestr();

The rest of your code is useless. To get correct get/set implementation, use something like this:

public str parmPrimaryKey(str _value = PrimaryKey.text())
    if (!prmIsDefault(_value))
    return _value;