Recent

Author Topic: DefineProperties and enumerated  (Read 449 times)

jmpessoa

  • Hero Member
  • *****
  • Posts: 1471
DefineProperties and enumerated
« on: October 21, 2019, 08:12:09 pm »
 
Hi, All!

I got this code by @tazz [from a very old post...]


Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.   { TMyComponent }
  12.   TMyComponent = class(TComponent)
  13.   private
  14.     FMyInt : Int64;
  15.     FMyIntArray : array of Integer;
  16.     procedure ReadMyInt(aReader: TReader);
  17.     procedure WriteMyInt(aWriter: TWriter);
  18.     procedure ReadMyIntArray(aReader: TReader);
  19.     procedure WriteMyIntArray(aWriter: TWriter);
  20.   public
  21.     procedure DefineProperties(Filer :TFiler); override;
  22.   end;
  23.  
  24. implementation
  25.  
  26. { TMyComponent }
  27.  
  28. procedure TMyComponent.ReadMyInt(aReader :TReader);
  29. begin
  30.   FMyInt := aReader.ReadInteger;
  31. end;
  32.  
  33. procedure TMyComponent.WriteMyInt(aWriter :TWriter);
  34. begin
  35.   aWriter.WriteInteger(FMyInt);
  36. end;
  37.  
  38. procedure TMyComponent.ReadMyIntArray(aReader :TReader);
  39. var
  40.   vLen:Integer;
  41. begin
  42.   vLen := aReader.ReadInteger;
  43.   SetLength(FMyIntArray,vLen);
  44.   aReader.Read(FMyIntArray[0], vLen * SizeOf(Integer));
  45. end;
  46.  
  47. procedure TMyComponent.WriteMyIntArray(aWriter :TWriter);
  48. begin
  49.   aWriter.WriteInteger(Length(FMyIntArray));
  50.   aWriter.Write(FMyIntArray[0],Length(FMyIntArray)*SizeOf(Integer));
  51. end;
  52.  
  53. procedure TMyComponent.DefineProperties(Filer :TFiler);
  54. begin
  55.   inherited DefineProperties(Filer);
  56.   Filer.Defineproperty('MyInt',@ReadMyInt,@WriteMyInt, True);
  57.   Filer.Defineproperty('MyArray',@ReadMyIntArray,@WriteMyIntArray, Length(FMyIntArray)>0);
  58. end;
  59.  
  60. end.
  61.  
  62.  

Can someone implement  a  "enumerated"  property example?

Thanks to All!

« Last Edit: October 21, 2019, 08:26:38 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

avk

  • Full Member
  • ***
  • Posts: 157
    • my self-education project
Re: DefineProperties and enumerated
« Reply #1 on: October 22, 2019, 07:33:29 am »
Maybe you mean "enumerable"?

jmpessoa

  • Hero Member
  • *****
  • Posts: 1471
Re: DefineProperties and enumerated
« Reply #2 on: October 22, 2019, 06:09:54 pm »

Yes, like this:

Code: Pascal  [Select]
  1. type
  2.   TMonthType = (January, February, March, April,May, June, July, August, September, October, November, December);
  3.  
  4.  TMyComponent = class(TComponent)
  5.   private
  6.  
  7.         FMyEnum:  TMonthType;
  8.         procedure ReadMyEnum(aReader: TReader);
  9.         procedure WriteMyEnum(aWriter: TWriter);
  10.         ...........
  11.          ...........
  12.    publicc
  13.       ...........
  14.   end;
  15.  
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

howardpc

  • Hero Member
  • *****
  • Posts: 3183
Re: DefineProperties and enumerated
« Reply #3 on: October 22, 2019, 06:28:35 pm »
I am not clear what you are missing.
The LCL already provides built-in code for published enumerated properties.
Something such as
Code: Pascal  [Select]
  1. unit MyComponent;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes;
  9.  
  10. type
  11.  
  12.   TMonthType = (January, February, March, April,May, June, July, August, September, October, November, December);
  13.  
  14.   TMyComponent = class(TComponent)
  15.   private
  16.     FMyEnum:  TMonthType;
  17.   published
  18.     property MyEnum: TMonthType read FMyEnum write FMyEnum stored True;
  19.   end;
  20.  
  21. procedure Register;
  22.  
  23. implementation
  24.  
  25. procedure Register;
  26. begin
  27.   RegisterComponents('MyComps',[TMyComponent]);
  28. end;
  29.  
  30. end.
gives a component with an enumerable property that works, for instance, in the object inspector without the need to further override TReader/TWriter methods.

jmpessoa

  • Hero Member
  • *****
  • Posts: 1471
Re: DefineProperties and enumerated
« Reply #4 on: October 22, 2019, 06:45:07 pm »
Yes, @howardpc.

But I need hide some properties...

So I need the technique explains by @tazz as in the code up there .... ;D

Code: Pascal  [Select]
  1. procedure TMyComponent.ReadMyEnum(aReader :TReader);
  2. begin
  3.   FMyEnum:= aReader.??????????????????;  //<---there is not something like aReader.ReadEnum  ???
  4. end;
  5.  
  6. procedure TMyComponent.WriteMyEnum(aWriter :TWriter);
  7. begin
  8.   aWriter.Write????????(FMyEnum);// //<---there is not something like aWrite.WriteEnum(FMyEnum)  ???
  9. end;
  10.  
« Last Edit: October 22, 2019, 06:48:50 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

howardpc

  • Hero Member
  • *****
  • Posts: 3183
Re: DefineProperties and enumerated
« Reply #5 on: October 22, 2019, 08:05:37 pm »
The LResources unit has a RegisterPropertiesToSkip() routine.
Perhaps that would help you?