Recent

Author Topic: String from Status  (Read 2824 times)

Fahmy Rofiq

  • Jr. Member
  • **
  • Posts: 83
String from Status
« on: January 10, 2013, 06:38:43 am »
This is what i'm trying todo:

Code: [Select]
......
Type
  TMyStatus = (msOne, msTwo, msThree);
......
const
  MyStatus: array[0..2] of string = ('First Status', 'Second Status', 'Third Status');
......
var
  ms: TMyStatus;
......
Label1.Caption := MyStatus[ms];


Is that the correct way??
Thanks
Lazarus Trunk + FPC Fixes 32bit
Windows 10 x64

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: String from Status
« Reply #1 on: January 10, 2013, 07:05:06 am »
I think you are after the following

Code: [Select]

type
  TMyStatus = (msFirst, msSecond, msThird, msLast);
  TMyStatusDescriptions = array[TMyStatus] of string;
const
  MyStatusDescriptions : TMyStatusDescriptions =('First', 'Second', 'Third', 'Last');

Function StatusDescription(const aStatus:TMyStatus):string;
begin
  Result := MyStatusDescriptions[aStatus];
end;


Now when you add a new item on the TMyStatus type MyStatusDescription will not compile until you add the desciption of the new item as well.

Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Fahmy Rofiq

  • Jr. Member
  • **
  • Posts: 83
Re: String from Status
« Reply #2 on: January 10, 2013, 07:30:10 am »
Thanks, your code work very well.

But i have another problem, when i use like this:

Code: [Select]
const
  FIRST_STATUS = 999;
  SECOND_STATUS = 1000;
  THIRD_STATUS = $5a55aa55;
  LAST_STATUS = $5a55aa56;
type
  TMyStatus = (
      msFirst=FIRST_STATUS,
      msSecond=SECOND_STATUS,
      msThird=THIRD_STATUS,
      msLast=LAST_STATUS
      );
  TMyStatusDescriptions = array[TMyStatus] of string;
const
  MyStatusDescriptions : TMyStatusDescriptions =('First', 'Second', 'Third', 'Last');

Function StatusDescription(const aStatus:TMyStatus):string;
begin
  Result := MyStatusDescriptions[aStatus];
end;

Then i get Error: Data element too large for TMyStatusDescriptions.
How to solve this?
Lazarus Trunk + FPC Fixes 32bit
Windows 10 x64

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: String from Status
« Reply #3 on: January 10, 2013, 09:26:35 am »
Code: [Select]
MyStatusValues = array[TMyStatus] of dword = (999, 1000, $5a55aa55, $5a55aa56);

  Result := MyStatusValues[aStatus];
Would this work? Having those just as values. Original status would go from 0..3 still. There are limitations and options to enums capacity, but it should automatically try to compile it with largest one.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: String from Status
« Reply #4 on: January 10, 2013, 12:41:38 pm »
Extended enum won't work fine with sets, due to its internal implementation as bitsets. You could try using a true map data structure (fgl has tfpgmap, fcl-stl has gmap and ghashmap, I suggest ghashmap), though you'll need to do the mapping at runtime.

 

TinyPortal © 2005-2018