Recent

Author Topic: type helpers usage - Lazarus 1.6RC1 - FPC 3.0  (Read 1421 times)

frakno

  • Jr. Member
  • **
  • Posts: 93
type helpers usage - Lazarus 1.6RC1 - FPC 3.0
« on: December 20, 2015, 04:16:31 pm »
I am playing around with type helper and how they are helpful for me.

In my project I need a lot of subdivision into different actions, and depending on an action I need values for e.g. names, column widths, basic values, etc.

I give you an example how I try to use helpers to have fewer efforts. I tried to integrate Arrays into the helpers methods.

Please give me some feedback whether my approach is good and what could bring disadvantages and whether there are better ways.

Code: Pascal  [Select][+][-]
  1. unit Unit1; {$mode objfpc}{$H+} {$modeswitch typehelpers}
  2. interface
  3.  
  4. uses Classes, StdCtrls, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs;
  5.  
  6. type TIntegerHelper = type helper for Integer
  7.        function Str: string; end;
  8.  
  9. type BUA = ( buaKauf, buaVkf, buaBewert, buaReal );
  10.      TBuchArtHelper  = type helper for BUA
  11.        function Int( AEnum : BUA ): Integer;
  12.        function Str( AEnum : BUA ): String;
  13.        function Val( AEnum : BUA ): Integer;
  14.      end;
  15.  
  16. type { TForm1 } TForm1 = class( TForm)
  17.     Button1 : TButton;
  18.     Button2 : TButton;
  19.     procedure Button1Click( Sender : TObject) ;
  20.     procedure Button2Click( Sender : TObject) ;
  21.   private
  22.   public
  23.   end;
  24.  
  25. var Form1 : TForm1;
  26.  
  27. implementation {$R *.lfm}
  28.  
  29. { TForm1 }
  30.  
  31. procedure TForm1.Button1Click( Sender : TObject) ;
  32.   var
  33.      intval: LongInt = 33;
  34.   begin
  35.     ShowMessage( intval.Str );
  36.     ShowMessage( ( intval * 3).Str );
  37.   end;
  38.  
  39. procedure TForm1.Button2Click( Sender : TObject);
  40. var itest : Integer = 3;
  41. begin
  42.    if BUA.Int( buaReal ) = itest then begin { some Code }  end;
  43.    ShowMessage(  BUA.Str( buaReal ) );
  44. end;
  45.  
  46. { TIntegerHelper }
  47.  
  48.   function TIntegerHelper.Str: string;
  49.   begin
  50.     Result := IntToStr(Self);
  51.   end;
  52.  
  53.   function TBuchArtHelper.Int( AEnum : BUA ): Integer;
  54.   begin
  55.     Result := ord( AEnum );
  56.   end;
  57.  
  58.   function TBuchArtHelper.Str( AEnum : BUA ): String;
  59.     const  description : Array[0..3] of String = ( 'Wertpapier-Kauf', 'Wertpapier-Verkauf', 'Werpapier-Bewertung', 'Wp-Real.Gewinne' );
  60.   begin Result := description[ord( AEnum )]; end;
  61.  
  62.   function TBuchArtHelper.Val( AEnum : BUA ): Integer;
  63.     const  ColumnWidth : Array[0..3] of Integer = ( 120, 120, 80, 80 );
  64.   begin Result := ColumnWidth[ord( AEnum )]; end;
  65.  
  66. end.
  67.  
Lazarus 4.0 FPC 3.2.2 Windows 10 and 11

 

TinyPortal © 2005-2018