1

I need to write some Delphi code, but I have no prior experience with Delphi. I've seen people writing some code, known as unit1 or unit2 and import it using the code inside them. So, can I see the unit as a class in Java or C#?

Akos K
  • 7,071
  • 3
  • 33
  • 46
diegoaguilar
  • 8,179
  • 14
  • 80
  • 129
  • 6
    This is no way to learn a new language. Get a good reference or book and take some time over your learning. – David Heffernan Jun 14 '13 at 22:32
  • 3
    You need to start with an intro to Pascal/Delphi. There's a free [Essential Pascal](http://www.marcocantu.com/epascal) book - look below the print book advertisement, or buy the book (it's inexpensive). There are Delphi tutorials available via Google and Bing searches, or the [Delphi Language Guide](http://docwiki.embarcadero.com/RADStudio/XE4/en/Delphi_Reference) in the documentation itself. – Ken White Jun 15 '13 at 00:33

3 Answers3

6

No. A unit is a source code file in Delphi. You can essentially think of it as a namespace whose scope is exactly the same as the current file.

Within a unit, you can define classes, with type definition syntax. It looks like this:

type
  TMyClass = class(TParentClass)
  private
    //private members go here
  protected
    //protected members go here
  public
    //public members go here
  end;

Any methods are declared below the type declaration, not inline, which makes the code easier to read because you can see the composition of a class at a glance instead of having to wade through its implementation.

Furthermore, each unit has two main sections, called interface and implementation. A type declaration can be placed in either section, but implementing code is not valid in interface. This allows for a language concept similar to Java's or C#'s public and private classes: any type declared in interface is visible to other units that use this unit ("public"), while any type declared in implementation is visible only within the same unit.

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
6

To add to Mason's answer - a general Unit structure looks something like this :

Unit UnitName;    
  interface
    //forward declaration of classes, methods, and variables)
    uses 
      //list of imported dependencies needed to satisfy interface declarations
      Windows, Messages, Classes;
    const 
      // global constants
      THE_NUMBER_THREE = 3;
    type // declaration and definition of classes, type aliases, etc 
      IDoSomething = Interface(IInterface)
        function GetIsFoo : Boolean;
        property isFoo : Boolean read GetIsFoo;
      end;
      TMyArray = Array [1..5] of double;
      TMyClass = Class(TObject)
        //class definition
        procedure DoThis(args : someType);
      end;
      TAnotherClass = Class(TSomethingElse)
        //class definition
      end;        
    //(global methods)
    function DoSomething(arg : Type) : returnType;        
    var  //global variables
      someGlobal : boolean;      
  implementation
    uses
      //list of imported dependencies needed to satisfy implementation
    const
      //global constants with unit scope (visible to units importing this one)
    type
      //same as above, only visible within this or importing units
    var
      //global variables with unit scope (visible to units importing this one) 
    procedure UnitProc(args:someType)
    begin
      //global method with unit scope, visible within this or importing units
      //note no forward declaration!
    end;       
    procedure TMyClass.DoThis(args : someType)
    begin
      //implement interface declarations
    end;
    function DoSomething(arg : Type) : returnType;    
    begin
      // do something
    end;
  initialization
    //global code - runs at application start 
  finalization
    //global code - runs at application end
end. // end of unit

Obviously, every unit does not need all of these sections, but I think these are all of the possible sections that can be included. It took me a while to figure all of this out when I first lept into Delphi and I probably would have done well with a map like this so I provide it in case it is helpful.

J...
  • 30,968
  • 6
  • 66
  • 143
2

Inside Unit of Delphi, or library of C++ Builder you can build more than one class at the same time. The IDEs for JAVA frequently uses one class to one file, it differs from delphi or C++ Builder but you can do this pratice to Delphi or C++ Builder too.

The classes in each language has your particularities. Is possible to think in POO for all in the same way but to implement it differs.