Recent

Author Topic: How to make a non-visible component?  (Read 7251 times)

bulrush

  • Jr. Member
  • **
  • Posts: 86
How to make a non-visible component?
« on: June 28, 2016, 06:38:52 pm »
I am trying to learn about non-visible components/library. I want to make a beginner's component to add 2 numbers. The input is 2 numbers, the output is the sum of the 2 numbers. So I will also have a method .Addit(x,y).

I found a tutorial on making a visual component but not a non-visual component. Can someone help me?

Thank you.
Lazarus 1.6.0, FPC 3.0.0, Win 7 64-bit, current Perl programmer.

mig-31

  • Sr. Member
  • ****
  • Posts: 305
Re: How to make a non-visible component?
« Reply #1 on: June 28, 2016, 06:41:55 pm »
Hi,

insteand of class(TComponent) use class or class(TObject)

Lazarus 2.2.6 - OpenSuse Leap 15.4, Mageia 8, CentOS 7

bulrush

  • Jr. Member
  • **
  • Posts: 86
Re: How to make a non-visible component?
« Reply #2 on: June 28, 2016, 08:30:00 pm »
Is there any tutorial on how to do this? Like how do I create a public property, set it, and get it? How do I create a public method?

It's been 20 years since I did this in Delphi I'm afraid.

Lazarus 1.6.0, FPC 3.0.0, Win 7 64-bit, current Perl programmer.

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: How to make a non-visible component?
« Reply #3 on: June 28, 2016, 08:54:30 pm »
Check this code:

TSum has a constructor with passes X, Y as values.

Properties X and Y to read and write.

Public method Sum that returns the sum.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.   private
  17.     { private declarations }
  18.   public
  19.     { public declarations }
  20.   end;
  21.  
  22.   { TSum }
  23.  
  24.   TSum = class(TObject)
  25.   private
  26.     fx: integer;
  27.     fy: integer;
  28.     procedure Setfx(AValue: integer);
  29.     procedure Setfy(AValue: integer);
  30.   public
  31.     constructor Create(x, y: integer);
  32.     property x: integer read fx write Setfx;
  33.     property y: integer read fy write Setfy;
  34.     function Sum: integer;
  35.   end;
  36.  
  37. var
  38.   Form1: TForm1;
  39.  
  40. implementation
  41.  
  42. {$R *.lfm}
  43.  
  44. { TForm1 }
  45.  
  46. procedure TForm1.FormCreate(Sender: TObject);
  47. var
  48.   sum: TSum;
  49.   res: integer;
  50. begin
  51.   sum := TSum.Create(7, 8);
  52.  
  53.   res := sum.Sum;
  54.   ShowMessage(IntToStr(res));
  55.  
  56.   sum.x:=10;
  57.   sum.y:=20;
  58.  
  59.   res := sum.Sum;
  60.   ShowMessage(OmtTpStr(res));
  61.  
  62.   sum.Free;
  63. end;
  64.  
  65. { TSum }
  66.  
  67. procedure TSum.Setfx(AValue: integer);
  68. begin
  69.   if fx=AValue then Exit;
  70.   fx:=AValue;
  71. end;
  72.  
  73. procedure TSum.Setfy(AValue: integer);
  74. begin
  75.   if fy=AValue then Exit;
  76.   fy:=AValue;
  77. end;
  78.  
  79. constructor TSum.Create(x, y: integer);
  80. begin
  81.   fx := x;
  82.   fy := y;
  83. end;
  84.  
  85. function TSum.Sum: integer;
  86. begin
  87.   result := fx + fy;
  88. end;
  89.  
  90. end.
  91.    

bulrush

  • Jr. Member
  • **
  • Posts: 86
Re: How to make a non-visible component?
« Reply #4 on: June 28, 2016, 08:59:18 pm »
In this page http://wiki.freepascal.org/Class, it says every class should have in it's .Create function, the 'inherited;' text. But yours doesn't.

Code: Pascal  [Select][+][-]
  1.     constructor TSum.Create(x, y: integer);
  2.     begin
  3.       fx := x;
  4.       fy := y;
  5.     end;
  6.  
     

So should 'inherited;' go right after the 'begin' here?

EDIT: Thank you for the code. I'm a visual learner so I look at code then arrange the hierarchy of parts in my head. That helps me understand the details and the big picture.
« Last Edit: June 29, 2016, 01:40:27 pm by bulrush »
Lazarus 1.6.0, FPC 3.0.0, Win 7 64-bit, current Perl programmer.

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: How to make a non-visible component?
« Reply #5 on: June 28, 2016, 09:19:08 pm »
Because is nothing being inherited in the constructor of TObject

Code: Pascal  [Select][+][-]
  1.       constructor TObject.Create;
  2.         begin
  3.         end;  

rvk

  • Hero Member
  • *****
  • Posts: 6171
Re: How to make a non-visible component?
« Reply #6 on: June 28, 2016, 10:49:17 pm »
Because is nothing being inherited in the constructor of TObject
Wouldn't it still be considered "good practice" to do the inherited part?

Maybe something is going to be added in the future to TObject.Create.
(not likely but shouldn't be ruled out by default)

http://stackoverflow.com/questions/772336/using-inherited-in-the-create-constructor-of-an-tobject
« Last Edit: June 28, 2016, 10:50:59 pm by rvk »

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: How to make a non-visible component?
« Reply #7 on: June 28, 2016, 11:35:35 pm »
Well If they add something to TObject.Create must be informed.

Inside Lazarus Classes inc units some objects use the inherited, some not, so is not really neccesary today.

Or they should add the inherited there too...

Code: Pascal  [Select][+][-]
  1. constructor TIntConst.Create(AIntegerType: PTypeInfo; AIdentToInt: TIdentToInt;
  2.   AIntToIdent: TIntToIdent);
  3. begin
  4.   IntegerType := AIntegerType;
  5.   IdentToIntFn := AIdentToInt;
  6.   IntToIdentFn := AIntToIdent;
  7. end;

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How to make a non-visible component?
« Reply #8 on: June 29, 2016, 09:31:23 am »
Inside Lazarus Classes inc units some objects use the inherited, some not, so is not really neccesary today.
Necessary.... ? No i guess not, if you don't mind crossing the OOP concept.

And that can be done any day  ;D

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to make a non-visible component?
« Reply #9 on: June 29, 2016, 11:15:52 am »
Wouldn't it still be considered "good practice" to do the inherited part?
Not really, as calling it is purely a choice. There are times when you DON'T want to call parent constructor and instead initializes properties by itself, while there are times when you only need to extend what the parent constructor has done.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: How to make a non-visible component?
« Reply #10 on: June 29, 2016, 01:43:36 pm »
I think that this would be a good tutorial to add to this wiki page.  If no-one does it, and you agree, I would add it.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How to make a non-visible component?
« Reply #11 on: June 29, 2016, 02:12:13 pm »
I think that this would be a good tutorial to add to this wiki page.  If no-one does it, and you agree, I would add it.
Confusing:
- Class
- Object Oriented Programming with Free Pascal and Lazarus
- Programmign Using Classes

The thread here on itself had nothing todo with non visual component writing sofar. But it would be good to have a page for that (i haven't searched for links on that specific topic i seem to be unable to find a specific wiki page on the subject of creating/writing non-visual components. Even though there is a page on how to write lazarus components)

Notice the differences mentioned on other online references
- Delphi wikia
- Dr.Bob
But, there are perhaps better resources then those mentioned here by me...
« Last Edit: June 29, 2016, 03:09:30 pm by molly »

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: How to make a non-visible component?
« Reply #12 on: July 01, 2016, 09:40:37 pm »
I think it's a good idea to add a non-visual example to that page, wich was my suggestion.  Unless I'm missing something.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How to make a non-visible component?
« Reply #13 on: July 01, 2016, 11:21:26 pm »
I think it's a good idea to add a non-visual example to that page, wich was my suggestion.  Unless I'm missing something.
Non-visual component <> class.

The former (non-visual component) should indeed be (more prominent) part of that page you linked to, the latter is already (albeit very briefly) covered by the other links as posted and, should imho not belong to the page you linked to.

Hence the links to Delphia/Dr.Bob that try to explain what non-visual components are.

Hopefully that makes thing more clear ?
« Last Edit: July 02, 2016, 12:09:41 am by molly »

bulrush

  • Jr. Member
  • **
  • Posts: 86
Re: How to make a non-visible component?
« Reply #14 on: July 04, 2016, 02:28:39 pm »
I think that this would be a good tutorial to add to this wiki page.  If no-one does it, and you agree, I would add it.

Nuno, please add the tutorial to the wiki. It's fine with me.
Lazarus 1.6.0, FPC 3.0.0, Win 7 64-bit, current Perl programmer.

 

TinyPortal © 2005-2018