how to make a type private to the unit but not defining it in the implementation section?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

how to make a type private to the unit but not defining it in the implementation section?

Dennis
unit frproxyserver;

{$mode objfpc}{$H+}

interface

uses
   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Grids,
   frBase;

type

   TMyStringGrid=class(TStringGrid) //how to I make this class visible
only to this unit?
   public
   end;

   { TProxyServerFrame }

   TProxyServerFrame = class(TBaseFrame)
     Panel_Top: TPanel;
     StringGrid1: TMyStringGrid;
   private

   public

   end;

implementation

{$R *.lfm}

end.

=============
My reason is I don't want to pollute the name space but if I put the
declaration of TMyStringGrid in the implementation section, it cannot be
used by the field in the class declaration of TProxyServerFrame.

if there a compiler directive that I can put around
TMyStringGrid to make it only visible to the unit frproxyserver?

Dennis
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: how to make a type private to the unit but not defining it in the implementation section?

Darius Blaszyk-2
Make stringgrid1 a pointer and put TMyStringGrid in the implementation section?

Op do 7 mrt. 2019 om 09:11 schreef Dennis <[hidden email]>
unit frproxyserver;

{$mode objfpc}{$H+}

interface

uses
   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Grids,
   frBase;

type

   TMyStringGrid=class(TStringGrid) //how to I make this class visible
only to this unit?
   public
   end;

   { TProxyServerFrame }

   TProxyServerFrame = class(TBaseFrame)
     Panel_Top: TPanel;
     StringGrid1: TMyStringGrid;
   private

   public

   end;

implementation

{$R *.lfm}

end.

=============
My reason is I don't want to pollute the name space but if I put the
declaration of TMyStringGrid in the implementation section, it cannot be
used by the field in the class declaration of TProxyServerFrame.

if there a compiler directive that I can put around
TMyStringGrid to make it only visible to the unit frproxyserver?

Dennis
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: how to make a type private to the unit but not defining it in the implementation section?

Michael Van Canneyt
In reply to this post by Dennis


On Thu, 7 Mar 2019, Dennis wrote:

> unit frproxyserver;
>
> {$mode objfpc}{$H+}
>
> interface
>
> uses
>   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Grids,
>   frBase;
>
> type
>
>   TMyStringGrid=class(TStringGrid) //how to I make this class visible
> only to this unit?
>   public
>   end;
>
>   { TProxyServerFrame }
>
>   TProxyServerFrame = class(TBaseFrame)
>     Panel_Top: TPanel;
>     StringGrid1: TMyStringGrid;
>   private
Use a nested class:

  TProxyServerFrame = class(TBaseFrame)
    Panel_Top: TPanel;
    StringGrid1: TMyStringGrid;
  private
    type
      TMyStringGrid=class(TStringGrid) //how to I make this class visible   only to this unit?
      public
        Procedure SomeMethod;
      end;
   end;

Implementation

Procedure TProxyServerFrame.TMyStringGrid.SomeMethod;

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: how to make a type private to the unit but not defining it in the implementation section?

Anthony Walter-3
type
  _PrivateData = class
    private type THidden = record Name: string; end;
  end;

... and later in the implementation section of the same unit ...

var H: _PrivateData.THidden; begin H.Name := 'Hello'; end;

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: how to make a type private to the unit but not defining it in the implementation section?

Dennis Poon


Anthony Walter wrote:

> type
>   _PrivateData = class
>     private type THidden = record Name: string; end;
>   end;
>
> ... and later in the implementation section of the same unit ...
>
> var H: _PrivateData.THidden; begin H.Name := 'Hello'; end;
>
>
But that will have the same problem of extra long method declaration name
   procedure _PrivateData.THidden.Method1;
   begin
   end;


Dennis

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: how to make a type private to the unit but not defining it in the implementation section?

Tony Whyman
In reply to this post by Dennis
I would use an "interface" type in this situation.

In the unit's "interface" section define the interface type and a
function to create a new instance of the interface.

In the unit's implementation define all your classes including a class
that provides the interface. The implementation of the function that
creates a new instance of the interface should create and return a new
instance of the class that provides the interface.

If you are using COM interfaces then you don't even need to explicitly
destroy the interface as they are automatically deleted when all
references go out of scope. if you use CORBA interfaces then your
interface definition should include a method to free the interface.

On 07/03/2019 08:10, Dennis wrote:

> unit frproxyserver;
>
> {$mode objfpc}{$H+}
>
> interface
>
> uses
>   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Grids,
>   frBase;
>
> type
>
>   TMyStringGrid=class(TStringGrid) //how to I make this class visible
> only to this unit?
>   public
>   end;
>
>   { TProxyServerFrame }
>
>   TProxyServerFrame = class(TBaseFrame)
>     Panel_Top: TPanel;
>     StringGrid1: TMyStringGrid;
>   private
>
>   public
>
>   end;
>
> implementation
>
> {$R *.lfm}
>
> end.
>
> =============
> My reason is I don't want to pollute the name space but if I put the
> declaration of TMyStringGrid in the implementation section, it cannot
> be used by the field in the class declaration of TProxyServerFrame.
>
> if there a compiler directive that I can put around
> TMyStringGrid to make it only visible to the unit frproxyserver?
>
> Dennis
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal