Recent

Author Topic: using stdconvs,convutils units in convertor demo  (Read 375 times)

alaa123456789

  • Full Member
  • ***
  • Posts: 232
  • Try your Best to learn & help others
    • youtube:
using stdconvs,convutils units in convertor demo
« 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

wp

  • Hero Member
  • *****
  • Posts: 10060
Re: using stdconvs,convutils units in convertor demo
« Reply #1 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.
« Last Edit: July 08, 2022, 10:53:29 pm by wp »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10291
  • FPC developer.

alaa123456789

  • Full Member
  • ***
  • Posts: 232
  • Try your Best to learn & help others
    • youtube:
Re: using stdconvs,convutils units in convertor demo
« Reply #3 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

 

TinyPortal © 2005-2018