Doubt in Weak Scope .

ajith

New Member
Hello,

Please help in understanding Weak Scope's definition. We know that For each loop is a weak scoped block.

Here is a sample code.
For each customer no-lock:
end. /*for each customer*/
message custumer.name .

But we will not able to see any cutomer record from the above code. Then how can we tell as 'for each' loop is a weak scoped block ?


Thanks,
Ajith
 
Your code will compile fine but will give runtime error stating that there is no customer record available and this is why a simple FOR EACH is a weak scope block; i.e., we can make a reference to that same buffer/record outside of the FOR EACH too. Had you scoped the records correctly then the code would not have compile at all and give an error. Ex:
Code:
DO FOR Customer :
        FOR EACH Customer NO-LOCK :
        END.
END.
DISPLAY Customer.NAME.

Here since you are strong scoping the record to the outer DO Block, any reference to that record outside of the DO block will raise a compile error.
 
Hello,

Thanks for your reply. I got your point.

I think, Eventhough we can make reference to the record outside the FOR EACH loop without compilation error, We cannot access the record outside the block. So is there specific use of WEAK SCOPED block ?

- Ajith
 
When you allow the FOR EACH to run to completion there is no customer record to display -- you finished processing them all, FOR EACH looked for the next one, there are no more, so it exits and no customer record is available to display.

But if you jump out of the loop while a record is available it is a different story:

Code:
for each customer no-lock:
  leave.
end.
message customer.name.

This illustrates why there is no compiler error and what the effect of "weak scope" is and how the record can be available outside the block
 
Back
Top