"
Supported by

Dynamically call classes and methods

Now, learn how to dynamically invoke a method.

Let’s go for it.

How to invoke an instance method:


CALL METHOD ref->(f)

How to invoke a static method:


CALL METHOD class=>(f) 
CALL METHOD (c)=>method
CALL METHOD (c)=>(f) 

And how to invoke a method of the same class:


CALL METHOD (f)
CALL METHOD ME->(f) 

Variable f is alphanumeric and holds the name of the method.
Variable c is also alphanumeric and holds the name of the class.

The related instructions manual is here.

As if it weren’t enough, you can also pass through parameters dynamically, as follows:


CALL METHOD (class)=>(meth) 
      PARAMETER-TABLE 
        t_param.

where t_param is an internal table of type ABAP_PARMBIND_TAB.

The related instructions manual is here.

You’ll find an example of this here.

Very useful. Obviously, besides more confusing, since the code cannot be compiled, it will be forcibly slower than normal static calls. But it’s still good to know, because this can come in hand.

Update: Bruno Esperança points out, and I agree, that these dynamic calls should only be done strictly when necessary, since they will not be found through the "where-used" functionality.

Thanks, José Faria for showing me this.

Greetings from Abapinho.

3 comentários a “Dynamically call classes and methods”

  1. Programista ABAP Diz:

    PARAMETER-TABLE could be used also with FUNCTION CALL same with EXCEPTION-TABLE

  2. Bruno Diz:

    Boas!

    Um pequeno “à parte”… já visto utilizado em situações em que não era nada necessário… e uma desvantagem disto é que depois não se consegue utilizar a funcionalidade “where-used” no método invocado. Acho que vale a pena manter isto em mente!

    Abraço,
    Bruno

  3. Nuno Godinho Diz:

    Obrigado Bruno, concordo contigo e acrescentei uma nota ao artigo a dizer isso.

Deixe um comentário


About Abapinho
Abapinho runs on WordPress
Articles (RSS) e Comments (RSS).