Results 1 to 5 of 5

Thread: Passing a procedure to another unit / form

  1. #1

    Passing a procedure to another unit / form

    I have looked into this before and I can't quite work out how to assign the procedure.

    Examples show something like this:-

    type TDebugProc = procedure(Text: string);

    This is in effect a forward declaration on Form2 for a procedure that Form1 will pass it.

    So I declare a variable on Form2:-

    var
    MyDebugProc: TDebugProc;

    And I can call it like this:-

    MyDebugProc('TEST MESSAGE');



    The question is how to I populate this variable on Form2, from Form1 when the procedure declaration on Form1 is:-

    TForm1.WriteDebug(Text: String);
    begin
    {DO SOME STUFF}
    end;

    ??

    On Form1 I want to do something like this:-

    Form2.MyDebugProc := @WriteDebug;
    http://www.c5software.co.uk (site is being developed at the moment)

  2. #2

    Passing a procedure to another unit / form

    Hi Gadget,

    Im not completly sure what you mean but if im guessing write then this may help..

    Form 2:
    [pascal]
    type
    DebugProc = procedure (Text: String);

    var
    Form2: TForm2;
    MyDebugProc: TDebugProc;
    [/pascal]

    Form 1:
    [pascal]
    procedure WriteDebug(Text: String);
    begin
    //
    end;
    [/pascal]

    And you just assign 'MyDebugProc' with 'WriteDebug':
    [pascal]
    Unit2.MyDebugProc := WriteDebug;
    [/pascal]

    Before you call MyDebugProc i recommend you check to see if it assigned with:
    [pascal]
    If Assigned(MyDebugProc) Then MyDebugProc(Text);
    [/pascal]

    I cant remember how it works, but if the above gives an error try changing:
    [pascal]
    type
    DebugProc = procedure (Text: String);

    [/pascal]
    To:
    [pascal]
    type
    DebugProc = procedure (Text: String) Of Object;
    [/pascal]
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  3. #3

    Passing a procedure to another unit / form

    [pascal]unit Unit1;
    interface
    type
    TDebugProc = procedure(Text: string) of object;

    TForm1 = class...
    procedure RealDebugProc(Text: string);
    end;
    implementation
    uses Unit2;
    begin
    Form2.DebugProc:= @Form1.RealDebugProc;
    end.

    ///////////////////////////////////////
    unit Unit2;
    interface
    uses Unit1;
    type
    TForm2 = class...
    DebugProc: TDebugProc;
    procedure DoSomething;
    end;
    implementation

    procedure TForm2.DoSomething;
    begin
    if Assigned(@DebugProc) then
    DebugProc('Output debug string');
    end;
    [/pascal]
    There are only 10 types of people in this world; those who understand binary and those who don't.

  4. #4

    Passing a procedure to another unit / form

    When I looked at the expression Assigned(@DebugProc) then I was pretty sure that this would always evaluate to true, because I thought that @DebugProc would return the address of the variable pointing to the procedure. But thats not the case here, Delphi help states this: "If F is a routine (a function or procedure), @F returns F?¢_Ts entry point."

    Always nice to learn new things.

  5. #5
    Anonymous
    Guest

    Passing a procedure to another unit / form

    Thanks =D

    That worked a treat (once I moved a few things around).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •