When START-OF-SELECTION is Not Required

来源:互联网 发布:怎么缴费过期域名 编辑:程序博客网 时间:2024/06/06 17:30

原文:http://www.gotothings.com/abap/when-start-of-selection-is-not-required.htm。

Can you why the following program is not working.  If I remove start-of-selection and what is the importance of start-of-selection...... 

how we come to know that we have to use start-of-selection here...if I put start-of-selection statement in any line apart from that line 

it shows error, why is it so?

如果我去掉下面程序的start-of-selection,这个程序就不能运行。

我怎么知道我们要在那用start-of-selection呢,如果我把start-of-selection放在其他地方,程序就会出错,为什么呢?

REPORT ZABAPOO .CLASS RAJ DEFINITION. PUBLIC SECTION. DATA: C TYPE I. METHODS : SUM IMPORTING A TYPE I B TYPE I EXPORTING C TYPE I. ENDCLASS.CLASS RAJ IMPLEMENTATION. METHOD SUM. C = A + B. ENDMETHOD. ENDCLASS.DATA: V1 TYPE I, V2 TYPE I, SUM TYPE I.DATA: OBJ TYPE REF TO RAJ. START-OF-SELECTION.CREATE OBJECT OBJ. V1 = 2. V2 = 5.CALL METHOD OBJ->SUM EXPORTING A = V1 B = V2 IMPORTING C = SUM.WRITE: SUM.


This is just to elaborate on why START-OF-SELECTION is not required if CLASS IMPLEMENTATION is put at the end of the code. 

这就要详细说说如果CLASS IMPLEMENTATION放到代码最后,为什么START-OF-SELECTION不需要写。

It is a very basic, yet interesting concept and not relevant to only Object Oriented ABAP.

这是个很基础的也很有趣的概念并不仅仅和abap面向对象有关

In an ABAP report, non declarative statements (means other than data: or type stmts)  that are not assigned to a processing block are never executed. Only exception to this are the statements between the REPORT or PROGRAM statement and the first processing block (say a subroutine). These statements are assigned to the default event  
START-OF-SELECTION.

在abap report程序里,非声明语句如果没有指定处理块/事件块,那么这些语句将不会被执行。唯一例外的是 在report或program 语句和第一个处理块(子程序)之间的那些代码,将会默认指定为START-OF-SELECTION事件。

For example see the follwoing code - 

例子看下面代码:

REPORT ZTEST.* Statements between the REPORT or PROGRAM statement and the first  * processing block  WRITE ' Welcome '.* First processing block  FORM proc_block1. WRITE 'Enjoy ABAP'. ENDFORM.* Statement not assigned to any processing block - NOT ACCESSIBLE      WRITE / 'Learn ABAP'. PERFORM proc_block1.
In the above program the 2 nd WRITE statement after the first processing block  
proc_block1 is not executed because it is not assigned to any processing block. 
We get a syntax error that "Statement not accessible". 

在上面程序中,在第一个处理块proc_block1之后的第二个write语句(WRITE / 'learn ABAP')将不会执行,因为它为指定为任何一个处理块,我们会得到"语句不能访问"的语法错误。

There are 2 ways to avoid this syntax error - 

这里有2中解决方法(这里结合它的例子)

Method 1 :

Assign the unaccessible statement to the START-OF-SELECTION block, by writing keyword START-OF-SELECTION just before it. Now START-OF-SELECTION block will contain (statements between the REPORT or PROGRAM statement and the first processing block) & (statements written after keyword START-OF-SELECTION)

方法 1:

通过在该语句前面写关键字 START-OF-SELECTION的方式,指定不能访问的语句为START-OF-SELECTION块。现在会有START-OF-SELECTION块(语句在report/program语句 和 第一个处理块之间)&(语句写在START-OF-SELECTION关键字之后)。

REPORT ZTEST.* Statements between the REPORT or PROGRAM statement and the first * processing block  WRITE ' Welcome '.* First processing block  FORM proc_block1. WRITE / 'Enjoy ABAP'. ENDFORM.* Assign the unaccessible statement to START OF SELECTION block  START-OF-SELECTION. WRITE / 'Learn ABAP'.PERFORM proc_block1.


Method 2 :

Write the first processing block (FORM proc_block1) at the end i.e. after all statements not assigned to a processing block. So all those statements will now fall under the exception category (i.e. statements between the REPORT or PROGRAM statement and the first processing block) and can be accessed.

方法 2:

将第一个处理块(FORM proc_block1)放在最后。如在所有未指定事件块的最后。这样这些代码就属于例外的部分(如语句在report/program 语句 和第一个处理块之间)并能被访问。

REPORT ZTEST.* Statements between the REPORT or PROGRAM statement and the first * processing block WRITE ' Welcome '.WRITE / 'Learn ABAP'.PERFORM proc_block1.* First processing block put after all the statements which are * not assigned to a processing block FORM proc_block1. WRITE / 'Enjoy ABAP'. ENDFORM.

Now this was a procedural program, thinking from ABAP Objects perspective, the CLASS IMPLEMENTATION is normally the first processing block (not CLASS DEFINITION because it is just a declaration). So on writing another non declarative statement like CREATE OBJECT after this processing block and not assigning this statement to another processign block, we get same syntax error that 'STATEMENT NOT ACCESSIBLE'. 

这是一个面向过程的程序,现在考虑abap对象,CLASS IMPLEMENTATION 通常是第一个处理块(不是CLASS DEFINITION因为它只是声明)。所以当在这个处理块之后编写其他如CREATE OBJECT 的非声明语句,并且这些语句又没有指定其他处理块,我们会得到相同的语法错误:“语句不能访问”。

To avoid this error we can use 2 ways as discussed above - either assign the statement to a START-OF-SELECTION block or put the first processing block   i.e. CLASS IMPLEMENTATION at the end of the code.

为了避免这个错误我们可以用上面讨论的2个方法-指定语句为START-OF-SELECTION事件块或者吧第一个处理块,如CLASS IMPLEMENTATION放到最后




















0 0
原创粉丝点击