Usage of widening cast in abap oops
hi experts,
I understood the widening cast concept. i want to know in what scenarios, the usage of widening cast makes sense. does the widening improve performance of the program? is there any scenario where we MUST use the widening cast in the logic? i want to understand how do we justify the usage of widening cast.
please do not give me links explaining widening case. I know what widening cast is, but want to understand the need for it and how it helps in programing.
thanks
As for the thread's question: you use it when you have a subclass instance being pointed at by a superclass reference, and you need to access subclass specific components.
Adding about widening / down cast:
I repeat: you don't have choice.
You actually do. Check the following example. BAPIRET2_T is a table type for BAPIRET2.
You can call the method YOU know the actual referenced object of a subclass supports dynamically, like this:
* Using downcasting
DATA lo_tabledescr TYPE REF TO cl_abap_tabledescr.
DATA lo_structdescr TYPE REF TO cl_abap_structdescr.
DATA lv_linetypename TYPE string.
TRY.
lo_tabledescr ?= cl_abap_typedescr=>describe_by_name( 'BAPIRET2_T' ).
lo_structdescr ?= lo_tabledescr->get_table_line_type( ).
CATCH cx_sy_move_cast_error.
MESSAGE 'Error in downcast!' TYPE 'A'.
ENDTRY.
lv_linetypename = lo_structdescr->get_relative_name( ).
WRITE: /, lv_linetypename.
* Alternative way using dynamic calling
DATA lo_typedescr_tab TYPE REF TO cl_abap_typedescr.
DATA lo_typedescr_str TYPE REF TO cl_abap_typedescr.
lo_typedescr_tab = cl_abap_typedescr=>describe_by_name( 'BAPIRET2_T' ).
TRY.
CALL METHOD lo_typedescr_tab->('GET_TABLE_LINE_TYPE')
RECEIVING
p_descr_ref = lo_typedescr_str.
CATCH cx_sy_dyn_call_error.
MESSAGE 'Error in dynamic call!' TYPE 'A'.
ENDTRY.
lv_linetypename = lo_typedescr_str->get_relative_name( ).
WRITE: /, lv_linetypename, '...again'.
Note that in either approach, you should catch the relevant exception(s) that may occur, unless you are certain about the object's actual subclass.
Edited by: Alejandro Bindi on Mar 31, 2010 5:47 PM
As you can see I got confused by the narrowing / widening terms as well. I also found out the terms upcast / downcast to be much easier to understand.
Similar Messages
-
Narrowing cast and widening cast
Hi All,
I want to know what is the use of a narrowing cast and a widening cast in ABAP objects.
Can someone please give an example ?
Regards,
AshishHi,
Check out this link, This will guide you on ABAP Objects
http://www.sap-press.de/katalog/buecher/htmlleseproben/gp/htmlprobID-28?GalileoSession=22448306A3l7UG82GU8#level3~3
Both narrow and wide casting are related to inheritance.
In Narrow casting, you assign a runtime object of subclass to runtime object of superclass. By this assignment , you can access your subclass by using runtime object of your superclass. But here the important point is that, you are assigning subclass reference to super class reference, so you can only access the inherited components of subclass through super class reference.
Suppose you have a super class lcl_vehicle. it has two subclasses lcl_car and lcl_truck.
r_vehicle = r_car.
In Wide casting, you assign a superclass reference to subclass reference . Before this, you do narrow casting,
Now when you assign a superclass reference to subclass reference. You can access the inherited as well as the specific components of subclass.
taking the same example,
r_car ?= r_vehicle.
'?=' wide cast operator.
Regards
Abhijeet -
Narrow Cast and Widening cast in OOPs
hi friends,
Can u please clear with the above two concepts ..... i have many doubts on this ...
first of all Y we need the concept of Narrow cast and widenning cast ?
Expecting your answers ...
Thanks in advance
Cheers
Kripa Rangachari .....hi Kripa,
Narrowing cast means that the assignment changes from a more specialized view (with visibility to more components) to a more generalized view (with visibility to fewer components).
Narrowing cast is also referred to as up cast . Up cast means that the static type of the target variable can only change to higher nodes from the static type of the source variable in the inheritance tree, but not vice versa.
<b>Reference variable of a class assigned to reference variable of class : object</b>
class c1 definition.
endclass.
class c1 implementation.
endclass.
class c2 definition inheriting from c1.
endclass.
class c2 implementation.
endclass.
start-of-selection.
data : oref1 type ref to c1,
oref2 tyep ref to c2.
oref1 = oref2.
<b>Widening Cast</b>
DATA: o_ref1 TYPE REF TO object, o_ref2 TYPE REF TO class.
o_ref2 ?= o_ref1.
CATH SYSTEM-EXCEPTIONS move_cast_error = 4.
o_ref2 ?= o_ref1.
ENDCATCH.
In some cases, you may wish to make an assignment in which the static type of the target variable is less general than the static type of the source variable. This is known as a widening cast.
The result of the assignment must still adhere to the rule that the static type of the target variable must be the same or more general than the dynamic type of the source variable.
However, this can only be checked during runtime. To avoid the check on static type, use the special casting operator ?= and catch the potential runtime error move_cast_error
Regards,
Richa -
Why the cast error generates only in Widening cast?
Hi Shyam,
WELCOME TO SDN!!!
Please check this link
http://abapprogramming.blogspot.com/2007/10/oops-abap-8.html
The widening cast in this case does not cause an error because the reference airplane actually points to an instance in the subclass lcl_cargo_airplane. The dynamic type is therefore u2018REF TO lcl_cargo_airplaneu2019.
Here the widening cast produces the MOVE_CAST_ERROR runtime error that can be caught with u201CCATCH ... ENDCATCHu201D, because the airplane reference does not point to an instance in the subclass lcl_cargo_airplane, but to a u201Cgeneral airplane objectu201D. Its dynamic type is therefore u2018REF TO lcl_airplaneu2019 and does not correspond to the reference type cargo_airplane.
The widening cast logically represents the opposite of the narrowing cast. The widening cast cannot be checked statically, only at runtime. The Cast Operator u201C?=u201D (or the equivalent u201CMOVE ... ?TO u2026u201D) must be used to make this visible.
With this kind of cast, a check is carried out at runtime to ensure that the current content of the source variable corresponds to the type requirements of the target variables. In this case therefore, it checks that the dynamic type of the source reference airplane is compatible with the static type of the target reference cargo_airplane. If it is, the assignment is carried out. Otherwise the catchable runtime error u201CMOVE_CAST_ERRORu201D occurs, and the original value of the target variable remains the same.
Best regards,
raam -
Narrow cast and wide cast in ABAP OO
Hi Xperts
Considering the code below can the concepts of narrowing cast and widening cast be explained ? Kindly help me in getting a clear picture of the usage of it.
CLASS C1 DEFINITION.
PUBLIC SECTION.
CLASS-DATA: COUNT TYPE I.
CLASS-METHODS: DISP.
ENDCLASS. "defintion C1
CLASS C1 IMPLEMENTATION.
METHOD DISP.
WRITE :/ COUNT.
ENDMETHOD. "disp
ENDCLASS. "c1 IMPLEMENTATION
CLASS C2 DEFINITION INHERITING FROM C1.
PUBLIC SECTION.
CLASS-METHODS: GET_PROP.
ENDCLASS. "c2 INHERITING C1
CLASS C2 IMPLEMENTATION.
METHOD GET_PROP.
COUNT = 9.
CALL METHOD DISP.
ENDMETHOD. "get_prop
ENDCLASS. "c2 IMPLEMENTATION
START-OF-SELECTION.
DATA: C1_REF TYPE REF TO C1,
C2_REF TYPE REF TO C2.
PS: Pls dont post any links as i already gone thru couple of those in SDN but couldnt get a clear infoHi
I got to know abt narrow cast: referecing a super class ref to a subclass ref so that the methods in subclass can be accessed by the super class reference. Below is snippet of the code i'd checked. Hope this is correct to my understanding. If any faults pls correct me. Also if anyone can explain wideing cast in this context will be very helpful
FOR NARROW CAST
{size:8}
CLASS C1 DEFINITION.
PUBLIC SECTION.
CLASS-DATA: COUNT TYPE I.
CLASS-METHODS: DISP_COUNT.
ENDCLASS. "defintion C1
CLASS C1 IMPLEMENTATION.
METHOD DISP_COUNT.
WRITE :/ 'Class C1', COUNT.
ENDMETHOD. "disp
ENDCLASS. "c1 IMPLEMENTATION
CLASS C2 DEFINITION INHERITING FROM C1.
PUBLIC SECTION.
CLASS-METHODS: GET_PROP,
DISP.
ENDCLASS. "c2 INHERITING C1
CLASS C2 IMPLEMENTATION.
METHOD GET_PROP.
COUNT = 9.
CALL METHOD DISP.
ENDMETHOD. "get_prop
METHOD DISP.
WRITE :/ 'Class C2', COUNT.
ENDMETHOD. "DISP
ENDCLASS. "c2 IMPLEMENTATION
START-OF-SELECTION.
DATA: C1_REF TYPE REF TO C1,
C2_REF TYPE REF TO C2.
CREATE OBJECT: C2_REF TYPE C2.
* CREATE OBJECT: C1_REF TYPE C1.
BREAK-POINT.
C1_REF = C2_REF.
CALL METHOD C1_REF->DISP_COUNT.
CALL METHOD C1_REF->('GET_PROP').
{size}
Edited by: Prabhu S on Mar 17, 2008 3:25 PM -
Narrow Casting Vs Widening Casting
Hello,
Can anybody please explain me the difference with an example between <b><i>Narrow Casting</i></b> and <i><b>Widening</b></i> <i><b>Casting</b></i> in ABAP Objects.
Regards
KalyanHi Kalyan
In fact, the names imply everything. Let me illustrate a bit. This illustration will deal with one aspect which I think will be enough at least to understand the concept simply. Think of two instances (objects) of two classes one of which is inherited from the other, that is; one is a general class and the other is a specialized class of it.
<u>e.g.</u>
Class 1 --> land_vehicle
Class 2 --> truck
The class <b>land_vehicle</b> has following attributes:
max_speed
number_of_wheels
number_plate
The class <b>truck</b> has following attributes:
max_speed [inherited]
number_of_wheels [inherited]
number_plate [inherited]
<i>load_capacity</i> [special to truck class]
Now, assume you have instantiated these classes as
<b>"land_vehicle_obj"</b> and <b>"truck_obj"</b> respectively from these classes.
Between these two objects, you can assign one to the other and at this point casting occurs.
i. If you assign <i>truck_obj</i> to <i>land_vehicle_obj</i> you lose the special attribute <i>load_capacity</i> since you go from the specialized on to the general one. Thus, this is a <b>narrowing cast</b>.
i. If you assign <i>land_vehicle_obj</i> and <i>truck_obj</i> you do not lose any attribute, nevertheless you add a new one. Thus this is a <b>widening cast</b>.
Regards
*--Serdar <a href="https://www.sdn.sap.com:443http://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.sdn.businesscard.sdnbusinesscard?u=qbk%2bsag%2bjiw%3d">[ BC ]</a> -
Change Narrowing and Widening Cast
I have ABAP Object 2007 book from Horst Keller and I also have a 7.0 SAP System.
SAP have changed the terminology from SAP 6.40.
Please see this hyperlink [Version 7.0 ABAP Modification 6|http://help.sap.com/abapdocu/en/ABENNEWS-70-DOCU.htm#!ABAP_MODIFICATION_6@6@]
In addition when I click on Help on MOVE and the Casting operator in my 7.0 Version system is explained as
Up Cast
Also referred to as widening cast. Assignment between reference variables, where the static type of the target variables is similar or identical to the static type of the source variables.
Down cast
Also called a narrowing cast, a down cast is an assignment between reference variables in which the typr static type of the target variable more specific than the static type of the source variable. A down cast is only possible in assignments with the casting operator (?=) or MOVE ... ?TO. Compare with up cast
SAP now want you to use the unambiguous terms of UP and DOWN Version 7.0 of course.
Does anybody have a pre640 SAP system (46C,47) that can confirm that the terminology has changed ?Hi,
The terminology has not changed, but its a prior mistake made while writing notes, but the concept still remains the same as far as I have check in systems...
Regards,
Siddarth -
Hi,
I am a debutant in ABAP OO. I am trying to understand the concept of Widening casting with lots of example found in SDN, but still I can't come to any conclusion on this.
Well I understood from one of the example that , before doing the widening , we have to do narrow casting..
DATA: lo_animal TYPE REF TO lcl_animal, " Super Class
lo_lion TYPE REF TO lcl_lion, " Sub class
lo_tmp_lion TYPE REF TO lcl_lion. " Sub class
CREATE OBJECT lo_tmp_lion.
lo_animal = lo_tmp_lion. " Narrow Casting
lo_lion ?= lo_animal. " Widening
Here my question is why do we have to assign the instance of lo_animal to lo_lion which is having the same instance of lo_lion( lo_lion & lo_tmp_lion are reference var of same class ). and i also found an another example from one of the SDN thread for widening which i really can't undersatnd.
DATA lo_tabledescr TYPE REF TO cl_abap_tabledescr.
DATA lo_structdescr TYPE REF TO cl_abap_structdescr.
DATA lv_linetypename TYPE string.
TRY.
lo_tabledescr ?= cl_abap_typedescr=>describe_by_name( 'BAPIRET2_T' ).
lo_structdescr ?= lo_tabledescr->get_table_line_type( ).
CATCH cx_sy_move_cast_error.
MESSAGE 'Error in downcast!' TYPE 'A'.
ENDTRY.
lv_linetypename = lo_structdescr->get_relative_name( ).
WRITE: /, lv_linetypename.
* Alternative way using dynamic calling
DATA lo_typedescr_tab TYPE REF TO cl_abap_typedescr.
DATA lo_typedescr_str TYPE REF TO cl_abap_typedescr.
lo_typedescr_tab = cl_abap_typedescr=>describe_by_name( 'BAPIRET2_T' ).
TRY.
CALL METHOD lo_typedescr_tab->('GET_TABLE_LINE_TYPE')
RECEIVING
p_descr_ref = lo_typedescr_str.
CATCH cx_sy_dyn_call_error.
MESSAGE 'Error in dynamic call!' TYPE 'A'.
ENDTRY.
lv_linetypename = lo_typedescr_str->get_relative_name( ).
WRITE: /, lv_linetypename, '...again'.
Please give me some hints to break through this.
Thanks in advace,
Eti.Hello Eti,
I think you're aware of Narrowing & Widening Cast, if not Arshad's response explains these concepts.
... why do we have to assign the instance of lo_animal to lo_lion which is having the same instance of lo_lion( lo_lion & lo_tmp_lion are reference var of same class ) ...
To understand this you need to know - [Static Type|http://help.sap.com/abapdocu_731/en/abenstatic_type_glosry.htm] and [Dynamic Type|http://help.sap.com/abapdocu_731/en/abendynamic_type_glosry.htm] for reference variables. (This applies to both data and object reference variables).
The golden-rule for assignment of reference variables is,
Dynamic type of a ref. variable must always be more specific than the static type.
DATA: lo_animal TYPE REF TO lcl_animal,"Super Class
lo_lion TYPE REF TO lcl_lion. "Sub Class
The static type of the ref. variable lo_lion is lcl_lion. Now let's consider this assignment,
lo_lion = lo_animal.
The compiler interprets the dynamic type of lo_lion (after this assignment) would be lcl_animal. It is more generic compared to the static type( lcl_lion ), since lcl_animal is a super-class of lcl_lion, so the compiler will throw a static syntax error. (Remember the "golden-rule" of reference variable assignment)
The [casting operator|http://help.sap.com/abapdocu_731/en/abencasting_operator_glosry.htm] is just an instruction to the compiler to skip the static syntax check during the widening cast,
lo_lion ?= lo_animal. "Widening Cast
Always remember that for Widening Casts the "golden-rule" of assignment will be checked during runtime.
If the rule is not satisfied, it'll result in an exception - CX_SY_MOVE_CAST_ERROR ! In order to avoid this situation, the demo program uses a temporary reference variable - lo_tmp_lion.
lo_animal = lo_tmp_lion. "Narrow Casting
lo_lion ?= lo_animal."Widening cast
Sorry for this big post, hope it helps & reduces your confusion
BR,
Suhas
PS: Normally when you're doing a Widening Cast you would not be required to use a temporary ref. variable. -
hi,
I saw sample application.
In that application do_init method have following code.
method do_init .
Create an instance of the search model
query ?= me->create_model(
class_name = 'ZCL_MODEL'
model_id = 'query' ).
endmethod.
Can anyone explain why we are using widening cast?Hi,
Go to ABAPDOCU tcode and see example programs in abap objects section, you will find separate programs for upcasting and downcasting .
Up-Cast (Widening Cast)
Variables of the type reference to superclass can also refer to subclass instances at runtime.
If you assign a subclass reference to a superclass reference, this ensures that
all components that can be accessed syntactically after the cast assignment are
actually available in the instance. The subclass always contains at least the same
components as the superclass. After all, the name and the signature of redefined
methods are identical.
The user can therefore address the subclass instance in the same way as the
superclass instance. However, he/she is restricted to using only the inherited
components.
In this example, after the assignment, the methods GET_MAKE, GET_COUNT,
DISPLAY_ATTRIBUTES, SET_ATTRIBUTES and ESTIMATE_FUEL of the
instance LCL_TRUCK can only be accessed using the reference R_VEHICLE.
If there are any restrictions regarding visibility, they are left unchanged. It is not
possible to access the specific components from the class LCL_TRUCK of the
instance (GET_CARGO in the above example) using the reference R_VEHICLE.
The view is thus usually narrowed (or at least unchanged). That is why we
describe this type of assignment of reference variables as up-cast. There is a
switch from a view of several components to a view of a few components. As
the target variable can accept more dynamic types in comparison to the source
variable, this assignment is also called Widening Cast
Static and Dynamic Types of References
A reference variable always has two types at runtime: static and dynamic.
In the example, LCL_VEHICLE is the static type of the variable R_VEHICLE.
Depending on the cast assignment, the dynamic type is either LCL_BUS or
LCL_TRUCK. In the ABAP Debugger, the dynamic type is specified in the form
of the following object display.
Reward Points if found helpfull..
Cheers,
Chandra Sekhar. -
What is upcast/ narrowing cast and widening cast/downcast?
Hi All SAPIENT,
What is upcast/ narrowing cast and widening cast/downcast?Why we are using upcast and downcast?Please send me good reply to my mail id pankaj.sinhas2gmail.com.
Regards
Pankaj Sinha.Hi Srinivasulu,
Narrowing cast means whenever Sub-Class Reference is Assigned to a Super Class reference it is Narrowing Cast,
Widening cast means Super Class reference is Assigned to a Sub-Class Reference then it is Widening Cast.
See the following links for more information,
[http://help-abap.blogspot.com/2008/09/abap-objects-narrowing-cast-upcasting.html]
[http://help-abap.blogspot.com/2008/09/abap-objects-widening-cast-down-casting.html]
Regards,
Raghava Channooru. -
Use of Widening Cast in Inheritence
Hello All,
Request you to help me in understanding the use of widening cast in Inheritence.
As of my understanding, before widening cast we need to copy the a subclass reference z_sub1 to a superclass reference z_super1 (to ensure that contents of superclass reference corresponds to the type requirement of the subclass reference used in widening cast). Then we assign the superclass referenece z_super1 to another subclass reference z_sub2.
data: z_sub1 type ref to zl_lcl_sub,
z_super1 type ref to zl_lcl_super,
z_sub2 type ref to zl_lcl_sub.
create object z_sub1.
z_super1 = z_sub1.
zsub2 ?= z_super1. " Widening cast
Now instaed of doing this, if we directly create an instance zsub3 of subclass, will it different from zsub2 obtained in widening cast.
data zsub3 type ref to zl_lcl_sub.
create object zsub3.
Does widening cast provide any additional advantage over this?
Regards
IndrajitDear Indrajit,
The basic aim of casting is to provide assignment between references.By this it means after assignment both the source & target will point to the same object.
As you said, if we use,
<b> data zsub3 type ref to zl_lcl_sub.
create object zsub3.</b>
it will cause a new object to be created of type ZL_LCL_SUB, which will be totally different from ZSUB2. The requirement here is to assign the reference of ZSUB2 to Z_SUPER1 so that both will pointer to the same object. If Widening Cast is not used then Syntax Check error will occur due to the fact that Z_SUB2 and Z_SUPER1 is not compatible.For normal reference assignment, the target (Z_SUB2) must be a subset of target i.e (Z_SUPER1).Which is not true in this case. Hence Widening Cast is used.
I hope its clear now....
Regards,
Samson. -
ABAP OOP example and invitation
For quite a while, I've been trying to learn more about ABAP OOP. But, I've
found many of the examples on the web, and in SAP provided articles, either
difficult to understand or outright lame. They never seemed to illustrate
much of what a staff ABAP programmer is called upon to do.
So, last week I decided to take the problem head on and see if I could come
up with something we all could use as a starting point to learn more about
ABAP objects.
Here's a chunk of code that I want everybody to pick apart, critique,
modify, append or whatever. The only stipulation is that you post your
suggestions back to this forum. Maybe after we get some nice ABAP objects
together, the forum manager can put them into an OOP area of this website.
================================================================
The first object I created uses the MARC (Plant Data for Material) table.
I wanted to use it to learn more about how "Selection Sets" can be used with
ABAP OOP.
The class has five methods:
-o- constructor
-o- return_marc_table
-o- return_subset_marc_table
-o- length
-o- write_marc_table
This class isn't, really, very useful. But, it does show how selection-sets
can be used with ABAP oop. The code is studded with commented BREAK-POINTS
that you can turn on and off to examine things.
Split the code into two files: Main program and Top include. Look at the
initialization section and you should be able to see how you can tweak
the materials selected for your environment.
enjoy
[email protected]
REPORT zejb_oop_sel_set2 .
This program is designed to demonstrate passing selection sets
to an ABAP object
The program has some educational value. But, other than that
probably isn't of much use.
=========================================== Main Program - BEGIN
INCLUDE zejb_oop_sel_set2_top. "data and class definitions
SELECT-OPTIONS:
s_matnr FOR marc-matnr,
s_werks FOR marc-werks.
DATA: my_marc TYPE REF TO marc_object .
============================================= START-OF-SELECTION
START-OF-SELECTION.
matnr_selector[] = s_matnr[] .
werks_selector[] = s_werks[].
call the constructor table and create the CLASS-DATA
CREATE OBJECT my_marc
EXPORTING
s_matnr = matnr_selector
s_werks = werks_selector .
================================================ END-OF-SELECTION
END-OF-SELECTION.
CALL METHOD my_marc->return_marc_table
IMPORTING
return_marc = tb_marc.
BREAK-POINT. " inspect tb_marc
CALL METHOD my_marc->length
IMPORTING
table_size = zzlines.
WRITE:/ 'Class-Data table in object my_marc has '
, zzlines , ' lines'.
ULINE.
WRITE:/ 'printing internal table tb_marc'.
CALL METHOD my_marc->write_marc_table
EXPORTING
print_table = tb_marc.
SKIP.
BREAK-POINT.
examine matnr_subset_selector
examine werks_subset_selector
CALL METHOD my_marc->return_subset_marc_table
EXPORTING
s_matnr = matnr_subset_selector
s_werks = werks_subset_selector
IMPORTING
return_marc = tb_subset_marc.
BREAK-POINT.
examine tb_subset_marc
ULINE.
WRITE:/ 'printing internal table tb_subset_marc'.
CALL METHOD my_marc->write_marc_table
EXPORTING
print_table = tb_subset_marc.
ULINE.
================================================== INITIALIZATION
INITIALIZATION.
DEFINE range_append. "#EC *
1 == range_name
clear &1. "#EC *
&1-sign = &2. "#EC *
&1-option = &3. "#EC *
&1-low = &4. "#EC *
&1-high = &5. "#EC *
append &1. clear &1. "#EC *
END-OF-DEFINITION. "#EC *
DEFINE fill_range. "#EC *
1 == range_name
clear &1. "#EC *
&1-sign = &2. "#EC *
&1-option = &3. "#EC *
&1-low = &4. "#EC *
&1-high = &5. "#EC *
END-OF-DEFINITION. "#EC *
modify these materials and plants to suit your environment
or create a selection variant
range_append s_matnr 'I' 'EQ' 'HAP100' space.
range_append s_matnr 'I' 'EQ' 'HAP205' space.
range_append s_matnr 'I' 'EQ' 'HAP221' space.
range_append s_matnr 'I' 'EQ' 'HAP240' space.
range_append s_matnr 'I' 'EQ' 'HAP245' space.
range_append s_matnr 'I' 'EQ' 'HAP250' space.
range_append s_matnr 'I' 'EQ' 'HAP260' space.
range_append s_werks 'I' 'EQ' '1000' space.
range_append s_werks 'I' 'EQ' '1020' space.
fill subset selector
fill_range st_matnr_selector 'I' 'EQ' 'HAP205' space.
APPEND st_matnr_selector TO matnr_subset_selector.
fill_range st_matnr_selector 'I' 'EQ' 'HAP250' space.
APPEND st_matnr_selector TO matnr_subset_selector.
fill_range st_werks_selector 'I' 'EQ' '1000' space.
APPEND st_werks_selector TO werks_subset_selector.
============================================ Main Program - End
*& Include ZEJB_OOP_SEL_SET2_TOP *
============================================ Top include - BEGIN
TABLES : marc.
DATA: tb_marc TYPE TABLE OF marc,
tb_subset_marc TYPE TABLE OF marc,
st_marc TYPE marc. "#EC *
"^^^^^^^ to/from structure. use as needed
DATA: st_matnr_selector TYPE range_s_matnr,
st_werks_selector TYPE range_werks,
matnr_selector TYPE TABLE OF range_s_matnr,
werks_selector TYPE TABLE OF range_werks,
matnr_subset_selector TYPE TABLE OF range_s_matnr,
werks_subset_selector TYPE TABLE OF range_werks.
DATA: zzlines TYPE i. " size of class_data table.
CLASS marc_object DEFINITION
NOTE: for those new to abap objects, EVERYTHING in abap OOP
must be explicitly "TYPED". more on this as I go along
CLASS marc_object DEFINITION.
PUBLIC SECTION.
TYPES:
typ_marc TYPE TABLE OF marc,
typ_matnr_selector TYPE TABLE OF range_s_matnr,
typ_werks_selector TYPE TABLE OF range_werks.
DATA: return_marc TYPE typ_marc,
print_table TYPE typ_marc.
CLASS-DATA:
class_marc
TYPE typ_marc. " <== inside public area
^^^^^^^ this table will be available to all methods
^^^^^^^ in the class
METHODS:
constructor
IMPORTING
s_matnr TYPE typ_matnr_selector
s_werks TYPE typ_werks_selector OPTIONAL ,
return_marc_table
EXPORTING return_marc TYPE typ_marc ,
return_subset_marc_table
IMPORTING
s_matnr TYPE typ_matnr_selector
s_werks TYPE typ_werks_selector OPTIONAL
EXPORTING
return_marc TYPE typ_marc,
length EXPORTING table_size TYPE i,
write_marc_table
IMPORTING print_table TYPE typ_marc.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS. "marc_object DEFINITION
*EJECT
CLASS zget_sales_order_change IMPLEMENTATION
CLASS marc_object IMPLEMENTATION.
METHOD constructor .
DATA:
s_lcl_matnr TYPE TABLE OF range_s_matnr ,
s_lcl_werks TYPE TABLE OF range_werks .
s_lcl_matnr[] = s_matnr[] .
s_lcl_werks[] = s_werks[] .
SELECT * FROM marc
APPENDING TABLE class_marc
WHERE matnr IN s_lcl_matnr
AND werks IN s_lcl_werks.
SORT class_marc BY matnr werks.
BREAK-POINT.
ENDMETHOD. "xxx
METHOD return_marc_table.
return_marc[] = class_marc[] .
BREAK-POINT. " examine class_marc
ENDMETHOD. "return_marc table
METHOD return_subset_marc_table.
DATA: st_marc TYPE marc.
REFRESH return_marc.
LOOP AT class_marc INTO st_marc
WHERE matnr IN s_matnr
AND werks IN s_werks .
APPEND st_marc TO return_marc.
ENDLOOP.
BREAK-POINT. " examine return_marc
ENDMETHOD. "return_subset_marc_table
METHOD length.
DESCRIBE TABLE class_marc LINES table_size.
ENDMETHOD. "length
METHOD write_marc_table.
DATA: st_marc TYPE marc.
LOOP AT print_table INTO st_marc.
WRITE:/ 'Material ' , st_marc-matnr,
'Plant ' , st_marc-werks.
ENDLOOP.
ENDMETHOD. "write_marc_table
ENDCLASS. "marc_object IMPLEMENTATION
============================================== Top include - ENDSearch using " ALV Factory methods" u'll get lots of material.
for instance u can a look at this wiki
https://www.sdn.sap.com/irj/sdn/wiki?path=/display/abap/abap%2b-%2bdeveloping%2binteractive%2balv%2breport%2busing%2booabap
https://www.sdn.sap.com/irj/sdn/wiki?path=/display/community/object%2bmodel%2balv%2b-%2binteractive
[ALVOO PDF|https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/cda3992d-0e01-0010-90b2-c4e1f899ac01] -
Scrolling Issue - ALV- ABAP OOPS!
Hello All,
I have a strange issue with regards to ALV developed using ABAP OOPS. I have more than 25 fields on the output screen.
For each field on output screen, F4 help is possible. So when I scroll to the right most fields on the screen, do a f4 help and fill the value, the alv output screen automatically moves to the left side.
I tried to resolve using SET_SCROLL_INFO_VIA_ID method of the class CL_GUI_ALV_GRID.
CALL METHOD g_alvgrid->set_scroll_info_via_id
EXPORTING
is_row_info = v_scrl_row_info
is_col_info = v_scrl_col_info.
* Set Scroll Position
CALL METHOD g_alvgrid->set_current_cell_via_id
EXPORTING
is_row_id = v_scrl_row_set
is_column_id = v_scrl_col_set.
But still moves to the left side.
If you have some ideas on this, pls share the same. Thanks in advance.
Best Regards
Himayat.
Edited by: Himayatullah Md on Nov 25, 2011 4:42 PM
Please use code tags
Edited by: Rob Burbank on Nov 25, 2011 10:49 AMIf you use [refresh_table_display|http://help.sap.com/saphelp_erp2004/helpdata/en/0a/b5531ed30911d2b467006094192fe3/frameset.htm] set paramter is_stable to keep scrollbar on desired position.
Regards
Marcin -
why u want Events and whts the use of it in ABAP OOPs concept
Locked for the same reason the others are
And please don't use textspeak.
Rob -
Hi,
This is chakravarthi. I have to learn abap-oops.
Sure! No Problem. You can find a classroom course and get on with it.
Edited by: kishan P on Apr 15, 2011 11:56 AMcheck ur fcat
Maybe you are looking for
-
How can i change the icloud id on an ipad in ios 8
My family of 4 were all sharing my apple ID so that we could all share our purchases. I have updated all of our iPads to iOS 8 and was really looking forward to the new family sharing features. However, on my husband's and sons' iPads, I cannot cha
-
How can I get rid of Nation as my tool bar search engine.
Greetings, I am extremely frustrated with Nation as my search engine on my Safari Web browser. Can someone please help me get rid of it? Here is a screen shot of what I see. I would really appreciate any support. Thank you, Balde
-
I have qt player 7 latest with updates. bought key for qt pro. registered using key. Window shown qt pro accepted key. Now I can't find pro. I have icon for qt player still. I click on icon and it opens a window for apple. How do I use pro? In progra
-
Add UDF to OPOR ,OINV ... via DI API
Hello... i want to add a User Defined Field to Documents via DI API. Is this possible? And if its possible, please show me a way to do it thx best regards Matthias
-
Aperture Metadata and Bridge Metadata
I am a photographer and my workflow is to import all my images into Aperture library and add metadata. When exporting an image is it possible to integrate Apertures metadata with Bridge metadata? This is important feature as most people do not import