Search code examples
delphiaccess-specifiersubroutine

Accessing Sub functions /procedures from DPR or other function / procedure in Delphi


As much I know - Subroutines are with Private access mode to its parent unction / procedure, right?

Is there any way to access them from "outer-world" - dpr or other function / procedure in unit?

Also - which way takes more calcualtion and space to compiled file?

for example:

function blablabla(parameter : tparameter) : abcde;
 procedure xyz(par_ : tpar_);
 begin
  // ...
 end;
begin
 // ...
end;

procedure albalbalb(param : tparam) : www;
begin
 xyz(par_ : tpar_); // is there any way to make this function public / published to access it therefore enabling to call it this way?
end;

// all text is random.

// also, is there way to call it from DPR in this manner?

// in C++ this can be done by specifing access mode and/or using "Friend" class .. but in DELPHI?

Solution

  • Note: Embedded Routines <> Private/Protected Methods.

    Embedded routines i.e. routines inside routines can not be accessed by external routines. You have posted an example of an Embedded routine, I also heard them called Internal Routines.

    Here is another example:

    procedure DoThis;
    
    function DoThat : Boolean;
    begin
      // This Routine is embedded or internal routine.
    end;
    begin
    
    // DoThat() can only be accessed from here no other place.
    
    end;
    

    Regardless of visibility, methods on classes, can be called using Delphi 2010 via RTTI. I have detailed how to do this in this article.

    If you are in the same Unit methods on a class can be accessed by any other code regardless of visibility, unless they are marked with Strict Private. This Question has more details and good example code in the accepted answer.

    If you are in two different units you can use the Protected Method Hack to access the protected methods. Which is detailed in detailed in this article.