Recent

Author Topic: How to merge multiple classes  (Read 943 times)

nikel

  • Sr. Member
  • ****
  • Posts: 276
How to merge multiple classes
« on: April 13, 2026, 04:44:46 am »
Hello, I'm trying to make a menu that would work in console / command prompt. I had to declare same class more than one time. Here's my code so far:

Code: Pascal  [Select][+][-]
  1. procedure TItem.Display;
  2. var
  3.   Key           : Char;
  4.  
  5. begin
  6.   repeat
  7.     ClrScr;
  8.     Writeln;Writeln;Writeln;
  9.  
  10.     WriteLn(self.Text);
  11.  
  12.     Key:=ReadKey;
  13.     if Key=#0 then
  14.       Key:=ReadKey;
  15.  
  16.     if (Key = #72) then
  17.     begin
  18.       // OnKeyPress(#72);
  19.     end
  20.     else if (Key = #80) then
  21.     begin
  22.       // OnKeyPress(#80);
  23.     end
  24.     else if (Key = #13) then
  25.     begin
  26.       // OnKeyPress(#13);
  27.       Exit;
  28.     end;
  29.   until Key = #13;
  30. end;
  31.  
  32. begin
  33.   I1:=TItem.Create('Please use arrows and enter your choice: ');
  34.   I1.Mark:=False;
  35.   I1.Name:='FirstSentence';
  36.  
  37.   I2:=TItem.Create(False, 'Separator', ' ');
  38.   I3:=TItem.Create(True, 'Opt1', 'Option 1'); // Mark = True
  39.   I4:=TItem.Create(False, 'Opt2', 'Option 2');
  40.   I5:=TItem.Create(False, 'Opt3', 'Option 3');
  41.   I6:=TItem.Create(False, 'Opt4', 'Option 4');
  42.   // Arr[0, 0]:=(Item);
  43.  
  44.   I2.Destroy;
  45.   I3.Destroy;
  46.   I4.Destroy;
  47.   I5.Destroy;
  48.   I6.Destroy;
  49.  
  50.   I1.Display;
  51.   I1.Destroy;
  52. end.

And the types

Code: Pascal  [Select][+][-]
  1. {TMenu}
  2.  
  3. type
  4.   TMenu = class
  5.   private
  6.     Name          : shortstring;
  7.     Text          : shortstring;
  8.   public
  9.     constructor Create(AName: shortstring; AText: shortstring);
  10.     destructor Destroy;
  11.  
  12.     procedure SetName(AName: shortstring);
  13.     function GetName(): shortstring;
  14.    
  15.     procedure SetText(AText: shortstring);
  16.     function GetText(): shortstring;
  17.  
  18.     procedure Display; virtual;
  19.   end;
  20.  
  21. {TItem}
  22.  
  23. type
  24.   TItem = class(TMenu)
  25.   private
  26.     Mark: Boolean;
  27.   public
  28.     constructor Create(AText: shortstring); overload;
  29.     constructor Create(AMark: Boolean; AName: shortstring; AText: shortstring); overload;
  30.     destructor Destroy; overload;
  31.  
  32.     procedure SetMark(AMark: Boolean);
  33.     function GetMark(): Boolean;
  34.  
  35.     procedure Display; override;
  36.   end;
  37.  
  38. const
  39.   ValidChars: set of char = ['0','1','2','3','4','5','6','7'];
  40.  
  41. var
  42.   I1, I2, I3, I4, I5, I6    : TItem;

I want the Display procedure to run once. I tried array of class but it didn't work.

I want to learn oop and event driven programming. How can I run the function once with oop principles?

cdbc

  • Hero Member
  • *****
  • Posts: 2772
    • http://www.cdbc.dk
Re: How to merge multiple classes
« Reply #1 on: April 13, 2026, 09:39:16 am »
Hi
Well, I can show you how I did it, by uploading a couple of files you can peruse and steal from  :D
· The help.crt.pas contains the meat
· The published project contains a test-project
· Last there's a string-manipulation-library, used in the above
Have fun & Regards -- Benny
« Last Edit: April 13, 2026, 09:41:43 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

cdbc

  • Hero Member
  • *****
  • Posts: 2772
    • http://www.cdbc.dk
Re: How to merge multiple classes
« Reply #2 on: April 13, 2026, 09:52:55 am »
Hi again
Just ran the test_crt and it's from the time just before I abandoned the 'crt' unit and went with 'Ansi-Escape-Sequences', so the framing is "Shijt" but the menu related stuff seems to work  %)
Regards Benny

edit: It should have looked something like the attached screenshot...
« Last Edit: April 13, 2026, 09:57:49 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

nikel

  • Sr. Member
  • ****
  • Posts: 276
Re: How to merge multiple classes
« Reply #3 on: April 13, 2026, 11:13:18 pm »
cdbc thank you for your reply, I couldn't compile it though.

cdbc

  • Hero Member
  • *****
  • Posts: 2772
    • http://www.cdbc.dk
Re: How to merge multiple classes
« Reply #4 on: April 14, 2026, 02:16:37 am »
Hi
I cannot read minds, so what is it complaining about, I'll happily provide any missing bits & pieces...
...But the layout was for you to look at some code and get ideas and maybe implement a missing function on the way... If you're trying with plugins enabled, you'll get exactly nowhere  ...and fast -- the framework hasn't been released yet.
I expect you to look at my code and immediately figure out, why it doesn't compile for you...  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

cdbc

  • Hero Member
  • *****
  • Posts: 2772
    • http://www.cdbc.dk
Re: How to merge multiple classes
« Reply #5 on: April 14, 2026, 11:57:56 am »
Hi
Well, it turns out, that I'm a goddamn pushover  :D
So I, sat down and pulled all the extras out of the test project, fiddled it into compilation(submission) and packaged it all up for your convenience and finally uploaded it here...  8-)
Don't come saying that this forum only takes...  :P
Regards /bc

edit: Fixed the compilation issue with "NowUTC" in 'help.crt.pas'
« Last Edit: April 15, 2026, 02:55:19 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

cdbc

  • Hero Member
  • *****
  • Posts: 2772
    • http://www.cdbc.dk
Re: How to merge multiple classes
« Reply #6 on: April 15, 2026, 12:48:42 pm »
Hi
If you have compilation issues with FPC 3.2.2 then just add this line in 'help.crt.pas:
Code: Pascal  [Select][+][-]
  1. ...
  2. implementation
  3. uses cdbc.advstring;
  4. /// HERE ///
  5. {$if FPC_FULLVERSION < 30301}function NowUTC: TDateTime; begin Result:= Now; end;{$ifend}
  6. /// HERE ///
  7. type
Regards Benny

edit: Fixed the compilation issue with "NowUTC" in 'help.crt.pas' in the new zip-file above
« Last Edit: April 15, 2026, 02:57:01 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

 

TinyPortal © 2005-2018