Forward refrencing

What is forward referencing and how to avoid this problem?

Forward referencing problem comes if you have forward reference and definition of class in two different scope. ( Here scope refers too $unit, package, module, interface, program). Both forward reference and definition should be in same scope.

In reply to vybhava:

Can you please elaborate more on it… I couldn’t get it. What is forward reference…

Hello pmehro,

When the compiler faces the handle of class declaration which is not declared yet when the other class using that handle. It will reports Error.

Lets say class Y using the handle x , which is not declared yet, so it will report an Error.

To Rectify This solution Typedef is used.

Here is the Example for better interpretation.

class Y ; 
X x; // refers to Class X, which is not yet defined 
endclass 

class X; 
int i; 
endclass

Below is the code to Resolve the Error.

typedef class X; 
class Y ; 
X x; // refers to Class X, which is not yet defined 
endclass 

class X; 
int i; 
endclass

In reply to ravichandrareddyp:

Changing the order of the class declarations will also fix the error, but sometimes you cannot always get the classes in the right order, especially when there are cross dependencies.

typedef class X; 
class Y ; 
X x; // refers to Class X, which is not yet defined 
endclass 
class X; 
int i;
Y y; 
endclass