Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: Spread code over multiple files

  1. #11

    Spread code over multiple files

    Hi Bijo,
    I hope I don't add more confusion, I used to teach Object Pascal, so I hope I can transfer some of the knowledge.

    Think of interface and implementation as areas of scope or visibility. Any code or definitions that you put in your interface section is accessible from other units. While code or definitions that you put in your implementation section is only accessible/visible to things within that, and only that unit.

    For example:
    [pascal]
    unit MyFirstUnit;

    interface

    procedure MyVisibleMethod1;

    procedure MyVisibleMethod2;


    implementation

    procedure MyInvisibleMethod1;
    begin
    // Do the lambada here
    end;

    procedure MyVisibleMethod1;
    begin
    // Call Method declared in my implementation section
    MyInvisibleMethod1;

    // Call Method declared in interface section
    MyVisibleMethod2;
    end;

    procedure MyVisibleMethod2;
    begin
    // Do the funky chicken here
    end;

    end.
    [/pascal]

    [pascal]
    unit MySecondUnit;

    interface

    procedure MyPhatMethod1;

    implementation

    uses
    MyFirstUnit; // If this is not here we cannot access the visible methods

    procedure MyPhatMethod1;
    begin
    // Now use the Visible Method from MyFirstUnit
    MyFirstUnit.MyVisibleMethod1;

    MyFirstUnit.MyVisibleMethod2;

    // The following method call will not work ( will cause a compilation error
    // because MyInvisiblerMethod1 is not accessible from this unit as it was
    // declared in the implementation section of the previous unit
    MyFirstUnit.MyInvisibleMethod1;
    end;

    end.
    [/pascal]

    You'll notice the I prepended each method call with MyFirstUnit and you are probably thinking man that is a pain, and you would be correct. But you actually don't need that there, I put it there to clarify my point of exactly which method we are calling. That notation is used in the rare cases where 2 units might have a method with the same name, so to avoid ambiguity you specify the unit name so that the compiler knows exactly which one is meant to be called at that point.

    I was going to type some stuff as to the reasons why you would put code in one part an not the other, but I think it is important to understand the concept of scope first before discussing the whys and why nots of putting code in the interface or implementation sections.

    I hope this helps rather than confuses.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  2. #12

    Spread code over multiple files

    I think I'm getting it now. Been reading your stuff, and rereading previous posts, and it's finally hitting me.

    I think what caused confusion were a couple factors: (1) my need or wish for an even compacter simpler language (Modulas or Oberons); (2) my never before having "declared functions twice" (if that's the proper description); (3) never really having used the scope principle (so far I've mainly used global variables, though I understood the local variable concept); (4) the code was not indented after putting the interface and implementation keywords (therefore automatically and visually I thought interface and implementation were empty, but all they do is signify a commence).


    ...Have read it again and now fully understand the general thing. Now it's just a matter of remembering it. Thanks, all of you! :) I think it'll be of good use in the future.

Page 2 of 2 FirstFirst 12

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
  •