Recent

Author Topic: Way to hide a class in the interface section of a unit?  (Read 1178 times)

jamie

  • Hero Member
  • *****
  • Posts: 6733
Way to hide a class in the interface section of a unit?
« on: September 25, 2023, 12:36:05 am »
I would like to hide access to a Class I wrote in the interface of a unit to be used only for the following class which is accessable from other units. I don't want this class to be used outside of the the unit but it needs to be in the interface section prior to the main class.

 I know I can create another unit that only this unit will use and thus this class will not be seen, but I want to keep all of this within the same unit.

 I guess what I need is a PRIVATE  statement like this.

 TmyClass = Class(...) Private

something like that, can I do this?
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 12457
Re: Way to hide a class in the interface section of a unit?
« Reply #1 on: September 25, 2023, 01:01:43 am »
I don't want this class to be used outside of the the unit but it needs to be in the interface section prior to the main class.
Why must the "hidden class" be in the interface section? Because the main class needs it? In this case I'd declare the "hidden class" in a rather general way as TObject or similar, fully implement it in the implementation section, and cast it to the implemented type whenever needed.

Code: Pascal  [Select][+][-]
  1. interface
  2. type
  3.   TMainClass = class (TSomething)
  4.   private
  5.     FHiddenClass: TObject;
  6.   ...
  7.     procedure NeedHiddenClass;
  8.   ....
  9.  
  10. implementation
  11.  
  12. type
  13.   THiddenClass = class(TSomethingToHide)
  14.     procedure DoSomething;
  15.   end;
  16.  
  17. constructor TMainClass.Create;
  18. begin
  19.   inherited;
  20.   FHiddenClass := THiddenClass.Create;
  21. end;
  22.  
  23. procedure TMainClass.NeedHiddenClass;
  24. begin
  25.   THiddenClass(FHiddenClass).DoSomething;
  26. end;
« Last Edit: September 25, 2023, 01:03:20 am by wp »

jamie

  • Hero Member
  • *****
  • Posts: 6733
Re: Way to hide a class in the interface section of a unit?
« Reply #2 on: September 25, 2023, 01:08:49 am »
I was trying to avoid the type casting.
 
If I have to, I will create another unit to house this one class.

Just thought maybe the compiler had a way to hide the class from outside access but still give full access to code within.

I guess I could use a WITH statement at the start of the mess for now.


The only true wisdom is knowing you know nothing

Eugene Loza

  • Hero Member
  • *****
  • Posts: 729
    • My games in Pascal
Re: Way to hide a class in the interface section of a unit?
« Reply #3 on: September 25, 2023, 01:16:49 am »
Not exactly this, but you can have a private class hidden inside of a public class itself:

Code: Pascal  [Select][+][-]
  1. type TMyPublicClass = class(TObject)
  2. strict private
  3.   type TMyPrivateClass = class(TObject)
  4.   public
  5.     procedure Conspiracy;
  6.   end;
  7.   var MyPrivateClass: TMyPrivateClass;
  8. public
  9.   procedure PublicProcedure;
  10. end;
  11.  
  12. procedure TMyPublicClass.PublicProcedure;
  13. begin
  14.   MyPrivateClass.Conspiracy;
  15. end;
  16.  
  17. procedure TMyPublicClass.TMyPrivateClass.Conspiracy;
  18. begin
  19.   WriteLn('Secret');
  20. end;
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

jamie

  • Hero Member
  • *****
  • Posts: 6733
Re: Way to hide a class in the interface section of a unit?
« Reply #4 on: September 25, 2023, 01:24:58 am »
Yes, I can do that, thank you.

I just hope the class won't get too large.

I was hoping to keep it outside of the main class.

Thanks.

The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 3618
Re: Way to hide a class in the interface section of a unit?
« Reply #5 on: September 25, 2023, 01:38:38 am »
I just hope the class won't get too large.
If it becomes too large then ... uhm ... you can situate that class inside its own little private unit . If not mistaken then that completes the circle :P  (sorry, couldn't help it).
This tagline is powered by AI

jamie

  • Hero Member
  • *****
  • Posts: 6733
The only true wisdom is knowing you know nothing

alpine

  • Hero Member
  • *****
  • Posts: 1289
Re: Way to hide a class in the interface section of a unit?
« Reply #7 on: September 25, 2023, 08:07:54 am »
I am trying to somewhat duplicate this
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.imageliststreamer?view=windowsdesktop-7.0
It is a sealed class with a private constructor. IMO both features are present in FPC too.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 7999
Re: Way to hide a class in the interface section of a unit?
« Reply #8 on: September 25, 2023, 08:28:24 am »
Why must the "hidden class" be in the interface section? Because the main class needs it? In this case I'd declare the "hidden class" in a rather general way as TObject or similar, fully implement it in the implementation section, and cast it to the implemented type whenever needed.

A class helper works well for that, I use it regularly for threads embedded in other objects.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018