Lazarus

Using the Lazarus IDE => General => Topic started by: alaa123456789 on July 08, 2022, 10:01:13 pm

Title: using stdconvs,convutils units in convertor demo
Post by: alaa123456789 on July 08, 2022, 10:01:13 pm
hi all ,

i am trying to make a demo for units convertor using the above mentioned units in uses section
after long research i reached to some knowledge about it (here incomplete code)
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, StdConvs, ConvUtils, Forms, Controls, Graphics, Dialogs,
  9.   StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     ComboBox1: TComboBox;
  17.     ListBox1: TListBox;
  18.  
  19.     procedure ComboBox1Change(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure changefamily(Sender: TObject);
  22.   private
  23.     fam: TConvFamily;
  24.     fams: TConvFamilyArray;
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.FormCreate(Sender: TObject);
  39. var
  40.  
  41.   len: integer;
  42.  
  43. begin
  44.   GetConvFamilies(fams);
  45.   Len := Length(Fams);
  46.   if (Len <> 0) then
  47.  
  48.     for len := Low(Fams) to High(Fams) do
  49.       //ComboBox1.Items.Add(format('%d: %s',[ord(fam),ConvFamilyToDescription(fam)]));
  50.       ComboBox1.Items.Add(format('%s', [ConvFamilyToDescription(len)]));
  51.   ComboBox1.ItemIndex := 0;
  52.   changefamily(self);
  53. end;
  54.  
  55. procedure TForm1.ComboBox1Change(Sender: TObject);
  56. begin
  57.   changefamily(self);
  58. end;
  59.  
  60.  
  61.  
  62. procedure TForm1.changefamily(Sender: TObject);
  63. var
  64.   atypes: TConvTypeArray;
  65.   i: integer;
  66. begin
  67.   //fggf
  68.   ListBox1.Clear;
  69.   fam := fams[ComboBox1.ItemIndex];
  70.   GetConvTypes(fam, atypes);
  71.   for i := Low(atypes) to High(atypes) do
  72.     ListBox1.Items.Add(format('%s', [ConvTypeToDescription(i)]));
  73.  
  74. end;
  75.  
  76. end.
  77.  
  78.  
but the results in the listbox are not correct (attched png)
what is wrong with it , here i attached also the demo project
please advise how to make it correct
thanks
Title: Re: using stdconvs,convutils units in convertor demo
Post by: wp on July 08, 2022, 10:44:27 pm
Rather than
Code: Pascal  [Select][+][-]
  1. procedure TForm1.changefamily(Sender: TObject);
  2. ...
  3.   for i := Low(atypes) to High(atypes) do
  4.     ListBox1.Items.Add(format('%s', [ConvTypeToDescription(i)]));
use
Code: Pascal  [Select][+][-]
  1.   for i in atypes do
  2.     ListBox1.Items.Add(ConvTypeToDescription(i));
This is because atypes is a TConvTypeArray which is nothing else than an array of integer. And Low() always returns 0 for a dynamic array. A bit strange kind of programming style was inherited from Delphi here...

BTW, there have been many changes recently in the conversion units, e.g. https://gitlab.com/freepascal.org/fpc/source/-/issues/39801, https://gitlab.com/freepascal.org/fpc/source/-/issues/39778, etc.
Title: Re: using stdconvs,convutils units in convertor demo
Post by: marcov on July 08, 2022, 11:31:21 pm

BTW, there have been many changes recently in the conversion units, e.g. https://gitlab.com/freepascal.org/fpc/source/-/issues/39801, https://gitlab.com/freepascal.org/fpc/source/-/issues/39778, etc.

http://www.stack.nl/~marcov/mergelogs32/convutils.html
Title: Re: using stdconvs,convutils units in convertor demo
Post by: alaa123456789 on July 09, 2022, 08:05:54 pm

This is because atypes is a TConvTypeArray which is nothing else than an array of integer. And Low() always returns 0 for a dynamic array. A bit strange kind of programming style was inherited from Delphi here...



thanks WP , for all people in this forum
please watch and download the full sample from here

https://youtu.be/ZBV_ijOoG7k (https://youtu.be/ZBV_ijOoG7k)
TinyPortal © 2005-2018