|
|
|
5 Most Popular Contributions
|
|
|
|
|
|
|
|
Written by Rich
|
|
Thursday, 17 May 2007 |
Macros and Performs (Forms or Procedures) are two of the techniques within ABAP that allow you to modularise your programs. Modularisation of programs allows for easier maintenance and reuse of code. These, along with Include Files and Function Modules provide the tools with which efficient, reusable code can be produced. What is the difference between a macro and a Perform ?
In technical terms, macros are expanded in-line, whereas a Perform is out of line.....  Macros have to be defined in the same way that Performs have to be with "Define" and "End-Of-Definition". They are generally used for short pieces of code that cannot be called using a perform. An Example. The subroutine "Subtitles" prints the report criteria based upon the values passed to in by the caller. The caller has to loop around the select-options table and pass the relevant data. This is due to the fact that although Select-Options tables have the same field names, the field definitions differ depending on the table you have set the select options up for. As you have to specify the structure in the Perform definition, this makes the writing of a generic routine using Performs impossible: Code: Form Subtitles Tables p_selects structure s_vkorg using p_title. The actual definition is: Code: FORM SUBTITLES using p_text p_sign p_option p_low p_high. This requires the calling program to loop around the relevant select options table: Code: Loop at s_vkorg. Perform Subtitles using "Title Text" s_vkorg-sign s_vkorg-option s_vkorg-low s_vkorg-high. EndLoop. Obviously when this occurs once, the few extra lines of code are no problem, however, eight or nine selection options can become a bore. By using a macro, this problem can be alleviated by expanding the code in-line so that all variables etc are available as normal. Code: Define PSub. Loop at &1. Perform Subtitles using &2 &1-Sign &1-option &1-low &1-high. EndLoop. End-Of-Definition. The macro expands any arguments by replacing the &n parameter with the relevant parameter passed by the calling program. Therefore, the lines of code above become: Code: Psub s_vkorg Text-021. In a real application, this would look like: Code: Top-Of-Page. * * If Only one company, use that otherwise use xxxxxxx * Describe table s_vkorg lines w_lines. if w_lines > 1. Perform Repheader using 'BG00' w_title. Else. if s_vkorg-high = ''. Perform RepHeader using s_vkorg-low w_title. Else. Perform Repheader using 'BG00' w_title. endif. endif. * * Print out the selection criteria * Psub s_vkorg text-023. Psub s_vkgrp text-024. Psub s_vkbur text-025. Psub s_vtweg text-026. Psub s_spart text-027. * * Material Selections * Perform Matsub. PSub s_audat text-029. Perform PrintTitles. Other Example Macros. Here are a few examples of macros that I use almost on a daily basis: Code: Define Declare_Itab. Data: t_&1 type standard table of &2 initial size 0, w_&1 type &2. End-Of-Definition. This macro declares a standard itab and workarea: Code: Declare_Itab: Order_Header VBAK. These macros position a radio button on the screen, either with the title text to the left or the right of the button Code: Define RadioLeft. Selection-Screen Begin Of Line. Selection-Screen Position &1. Selection-Screen Comment (&2) &3. Parameter &4 RadioButton Group &5. Selection-Screen End Of Line. End-Of-Definition. * Define RadioRight. Selection-Screen Begin Of Line. Selection-Screen Position &1. Parameter &4 RadioButton Group &5. Selection-Screen Comment (&2) &3. Selection-Screen End Of Line. End-Of-Definition.
RadioRight 20 15 Text-005 p_bttn Grp1.
Related Items:
|
|