‎2011 Apr 06 7:17 PM
Hi Folks,
I am new to the ABAP world and has started programming in it for about two weeks now (therefore, two weeks of experience so far ). I have a question regarding definition of complex types in ABAP. Basically what I want to achieve is to define a type which is either mytype1 or mytype2. During the runtime, it should be decided whether complextype is of mytype1 or of mytype2. Maybe to put some more concrete context: I will call a method with an object of type "complextype". Depending how I set this object, this will be of mytype1 or mytype2. Please see the code schema below.
TYPES BEGIN OF mytype1.
TYPES:
.......
.......
TYPES END OF mytype1.
TYPES BEGIN OF mytype2.
TYPES:
.......
.......
TYPES END OF mytype2.
TYPES BEGIN OF complextype.
TYPES:
mytype_name TYPE mytype1,
mytype_name TYPE mytype2.
TYPES END OF complextype.
Is this a correct way to approach? If yes, how can I express this (either mytype1 or mytype2 but only one of them at a certain time) in ABAP types correctly?
I will appreciate any kind of help.
Thanks a million,
Boldbaatar
Edited by: Boldbaatar Tsend-Ayush on Apr 6, 2011 8:24 PM
‎2011 Apr 06 8:15 PM
Hi,
As for the complex dynamic types (available at runtime) you will have to define such type also dynamically. For this you can refer my blog [Do you really know everything about typing? - part 2|http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/20887] [original link is broken] [original link is broken] [original link is broken];. Here you get the explanation how to define such types during runtime.
The second issue I can see here is
During the runtime, it should be decided whether complextype is of mytype1 or of mytype2 (...) I will call a method with an object of type "complextype".
You can only work with dynamic structures and/or define them, but you can't actually type the parameter of dynamic structure. Even you tell the system that you want to use complex type as parameter type, system will statically (during syntax check) check whether it actual parameter passed to it really comply this type (can't be either mytype1 or mytyp2). So there is no way to do that. An alternative, and the only way I can think of, is generic or semi generic parameter typing. For details refer next blog from the series [Do you really know everything about typing? - part 4|http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/21128] [original link is broken] [original link is broken] [original link is broken];.
If you still have questions in this regard, don't hesitate to ask.
Regards
Marcin
‎2011 Apr 06 8:15 PM
Hi,
As for the complex dynamic types (available at runtime) you will have to define such type also dynamically. For this you can refer my blog [Do you really know everything about typing? - part 2|http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/20887] [original link is broken] [original link is broken] [original link is broken];. Here you get the explanation how to define such types during runtime.
The second issue I can see here is
During the runtime, it should be decided whether complextype is of mytype1 or of mytype2 (...) I will call a method with an object of type "complextype".
You can only work with dynamic structures and/or define them, but you can't actually type the parameter of dynamic structure. Even you tell the system that you want to use complex type as parameter type, system will statically (during syntax check) check whether it actual parameter passed to it really comply this type (can't be either mytype1 or mytyp2). So there is no way to do that. An alternative, and the only way I can think of, is generic or semi generic parameter typing. For details refer next blog from the series [Do you really know everything about typing? - part 4|http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/21128] [original link is broken] [original link is broken] [original link is broken];.
If you still have questions in this regard, don't hesitate to ask.
Regards
Marcin
‎2011 Apr 06 8:23 PM
Hello Marcin,
thanks for the prompt response. I will read your blog and may come up with new questions.
Best,
Boldbaatar
‎2011 Apr 07 7:53 AM
Hi,
Could you explain in some more details what do you want to achieve with your design?
Maybe defining two optional parameters and checking them with
IS SUPPLIEDwould be enough for your method.
I could also think about defining a class with two subclasses and passing reference to the parent but it all depends on your particular requirements.
‎2011 Apr 11 6:53 PM
Hi,
thanks for the response. At the moment, your solution is doing the job for me. I just pass the objects of different types together when I call the method. Depending on which type to be used, I just look whether it is supplied or not. For my situation of having only two different types, I find your solution good and simple.
Best,
Boldbaatar
‎2011 Apr 07 9:12 AM
Hi,
I think you should use the dynamic data reference .
TYPES BEGIN OF mytype1.
TYPES:
.......
.......
TYPES END OF mytype1.
TYPES BEGIN OF mytype2.
TYPES:
.......
.......
TYPES END OF mytype2.
data: dref_complex type ref to data.
field-sumbols:<fs_any> type any.
if type eq mytype1.
l_type = 'MY_TYPE1'.
elseif type eq mytype2.
l_type = 'MY_TYPE2'.
endif.
create data dref_complex type (l_type).
"In order to access the components of dref_complex , you should assign the same to field symbol
assing dref->* to <fs_any>.
"then u can use ASSIGN COMPONENT to get individual components
Regards
Arshad
‎2011 Apr 07 9:33 AM
Arshad,
Your code will give a runtime error!
One cannot create dynamic data objects using local types. (I mean the way you've mentioned in your code snippet). Read the documentation on CREATE DATA & get your basics clear.
BR,
Suhas
‎2011 Apr 07 10:33 AM
>
> Arshad,
>
> Your code will give a runtime error!
>
> One cannot create dynamic data objects using local types. (I mean the way you've mentioned in your code snippet). Read the documentation on CREATE DATA & get your basics clear.
>
> BR,
> Suhas
Hi Suhas,
I must disagree. Typing of data references is the same as you would be typing local data objects, only data representation differs (in first case it is data object of some type, in the second it is a reference to such data object of some type). So you are able to type it both with DDIC and local types.
Regards
Marcin
‎2011 Apr 07 10:53 AM
Hi Marcin & Arshad,
... type can be any data type from the ABAP Dictionary - especially the structure of a database table, a public data type of a global class, or any data type of the same program that has already been defined with TYPES ...
My Bad !!!
Thanks,
Suhas