You need some structure types that you can access in several global classes. You decide to define structure types in the ABAP Dictionary.
Hint
Before you define structure types in the ABAP Dictionary, you should investigate the option to define public structure types in one of the global classes or in a global interface.Task 1: Define a Simple Structure
Create a copy of global class /LRN/CL_S4D430_TYT_STRUCTURE (suggested name: ZCL_##_STRUCTURE, where ## is your group number). Replace structure type st_address with a dictionary structure (suggested name: Z##S_ADDRESS).
Steps
Copy class /LRN/CL_S4D430_TYT_STRUCTURE to a class in your own package (suggested name: ZCL_##_STRUCTURE, where ## is your group number).
In the Project Explorer view, right-click class /LRN/CL_S4D430_TYT_STRUCTURE to open the context menu.
From the context menu, choose Duplicate ....
Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_STRUCTURE, where ## stands for your group number.
Adjust the description and choose Next.
Confirm the transport request and choose Finish.
In the PUBLIC SECTION of the class definition, analyze the definition of type st_address.
Perform this step as you have done earlier.
Create a new dictionary object of type Structure (suggested name: Z##S_ADDRESS).
In the Project Explorer view, expand your package and open the context menu on subnode Dictionary.
Choose New → Structure.
Confirm the package, enter the name Z##S_ADDRESS and the description Address, then choose Next.
Assign the new object to a transport request and choose Finish.
Remove the placeholder component_to_be_Changed : abap.string(0);from the component list of the structure. Then replace it with a component street. Use the same data element as in the st_address type in your global class.
Adjust the code as follows:
12345
define structure z##s_address {
}
Add components for the postal code, the city, and the country. Use the same component names and data elements as in the st_address type in your global class.
Adjust the code as follows:
123456
define structure z##s_address {
street : /dmo/street;
}
Activate the structure type.
Press Ctrl + F3 to activate the development object.
Return to the source code of your global class. In the implementation of the IF_OO_ADT_CLASSRUN~MAIN method, replace st_address with your new dictionary structure type. Make sure there are no syntax errors.
Adjust the code as follows:
12345678910
* Task 1
*****************************************************************
* DATA address TYPE st_address.
address-street = 'Dietmar-Hopp-Allee 16'.
address-postal_code = '69190'.
address-city = 'Walldorf'.
address-country = 'DE'.
Task 2: Define a Nested Structure
In your global class, remove or comment the public structure type st_person and replace it with a dictionary structure (suggested name: Z##S_PERSON). For the address, re-use the structure type you already created. For the name, use a quick fix to create a second simple structure with two components (suggested name: Z##S_NAME).
Steps
Create a new dictionary object of type structure.
In the Project Explorer view, expand your package and open the context menu on the Dictionary subnode .
Choose New → Structure.
Confirm the package, enter the name Z##S_PERSON and the description Person (Name and Address), then choose Next.
Assign the new object to a transport request and choose Finish.
Remove the placeholder component_to_be_Changed : abap.string(0); from the component list of the structure and replace it with a component for the address (suggested name: address). Type the new component with the structure that you created in the previous task (Z##S_ADDRESS).
Adjust the code as follows:
12345
define structure z##s_person {
}
Add another component for the name of the person (suggested name: Name). Enter Z##S_NAME as the name of the component type.
Note
This structure type does not yet exist. You create it in the next step.Adjust the code as follows:
12345
define structure z##s_person {
}
Save the structure type. Then use a quick fix to create structure type Z##S_NAME.
Press Ctrl + S to save the development object.
Left-click on the error icon next to the code line you just added and choose Create Dictionary Structure ....
Enter the description Name and choose Next.
Assign the structure to a transport request and choose Finish.
Remove the placeholder component_to_be_Changed : abap.string(0);from the component list of the structure and replace it with a component for the first name (suggested name: first_name) and a component for the last name (suggested name: last_name). Use suitable data elements from the /DMO/ name space as components types.
Adjust the code as follows:
12345
define structure z##s_name {
}
Save the structure type. Then activate all inactive development objects.
Press Ctrl + S to save the development object.
From the eclipse toolbar, choose Activate inactive ABAP development objects. Alternatively, press Ctrl + Shift + F3.
Choose Select All and Activate.
Return to the source code of your global class. In the PUBLIC SECTION of the class definition, remove or comment the definition of the types st_name and st_person.
Perform this step as before.
In the implementation of the IF_OO_ADT_CLASSRUN~MAIN method, replace st_person with your new dictionary structure type. Make sure there are no syntax errors.
Adjust the code as follows:
1234567891011
* Task 2
***********************************************************
* DATA person TYPE st_person.
person-name-first_name = 'Dictionary'.
person-name-last_name = 'ABAP'.
person-address-street = 'Dietmar-Hopp-Allee 16'.
person-address-postal_code = '69190'.
person-address-city = 'Walldorf'.
person-address-country = 'DE'.
Task 3: Define Named Includes
In your global class, remove or comment public structure type st_person_inc and replace it with a dictionary structure (suggested name: Z##S_PERSON_INC). Instead of structured components NAME and ADDRESS define named includes of the same name.
Steps
Create a copy of your nested dictionary structure (suggested name: Z##S_PERSON_INC, where ## is your group number).
In the Project Explorer view, expand your package and subnode Dictionary → Structure.
Open the context menu on structure type Z##S_PERSON and choose Duplicate.
Enter the name Z##S_PERSON_INC and choose Next.
Assign the new development object to a transport request and choose Finish.
Turn structured component name into a named include.
After the colon that separates the component name and the component type, insert keyword INCLUDE.
Repeat this for structured component address.
Make sure the structure type now looks like this:
123456
define structure z##s_person_inc {
name : z##s_name;
address : z##s_address;
}
Activate the structure type.
Press Ctrl + F3 to activate the development object.
Return to the source code of your global class. In the PUBLIC SECTION of the class definition, remove or comment the definition of type st_person_inc.
Perform this step as before.
In the implementation of the IF_OO_ADT_CLASSRUN~MAIN method, replace st_person_inc with your new dictionary structure type. Make sure both alternatives for accessing the components work.
Adjust the code as follows:
1234567891011121314151617181920
* Task 3
**********************************************************************
* DATA person2 TYPE st_person_inc.
person2-name-first_name = 'Dictionary'.
person2-name-last_name = 'ABAP'.
person2-address-street = 'Dietmar-Hopp-Allee 16'.
person2-address-postal_code = '69190'.
person2-address-city = 'Walldorf'.
person2-address-country = 'DE'.
* or -------------------------------------------------------
person2-first_name = 'Dictionary'.
person2-last_name = 'ABAP'.
person2-street = 'Dietmar-Hopp-Allee 16'.
person2-postal_code = '69190'.
person2-city = 'Walldorf'.
person2-country = 'DE'.
Activate and debug the class as a console app. Analyze the content of the data objects address, person, and person2.
Press Ctrl + F3 to activate the development object.
Press F9 to execute the class as console app.