Recent

Author Topic: [Solved] Type to String  (Read 1613 times)

bdexterholland

  • Jr. Member
  • **
  • Posts: 65
  • uh?
[Solved] Type to String
« on: September 30, 2019, 04:54:40 pm »
Hi....


supposing that i have a Type like this:
Code: Pascal  [Select][+][-]
  1. Type
  2.   TMyType = (mtFirstType, mtSecondType);

I wanna write a 'ToString' function to it, like:
Code: Pascal  [Select][+][-]
  1. function ToString();
  2. begin
  3.   case TMyType of
  4.     mtFirstType : result := 'My First Type';
  5.     mtSecondType : result := 'My Second Type';
  6.   end;

To use like on this example:
Code: Pascal  [Select][+][-]
  1.   myClass = class(TObject)
  2.     _MyType: TMyType;
  3.   end;
  4. implementation
  5. begin
  6.   myObject._MyType := mtFirstType;
  7.   writeln(myClass._MyType.ToString();


How can I write a procedure for a type? I don't wanna write to the class, i wanna create a ToString for a Type....
« Last Edit: September 30, 2019, 11:28:48 pm by bdexterholland »
[sleep .....]

JdeHaan

  • Full Member
  • ***
  • Posts: 115
Re: Type to String
« Reply #1 on: September 30, 2019, 05:08:53 pm »
something like this:


Code: Pascal  [Select][+][-]
  1. {$modeswitch typehelpers}
  2.  
  3. Type
  4.   TMyType = (mtFirstType, mtSecondType);
  5.   TMyTypeHelper = type helper for TMyType
  6.     function ToString: String;
  7.   end;
  8.  
  9. function TMyTypeHelper.ToString: String;
  10. begin
  11.   WriteStr(Result, Self);
  12. end;
  13.  

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Type to String
« Reply #2 on: September 30, 2019, 05:09:28 pm »
I haven't tested, but I believe it should be:

Code: Pascal  [Select][+][-]
  1. {$MODESWITCH TYPEHELPERS}
  2.  
  3. type
  4.   TMyType = (mtFirstType, mtSecondType);
  5.   TMyTypeHelper = type Helper for TMyType
  6.   public
  7.     function ToString: string;
  8.   end;
  9.  
  10. function TMyTypeHelper.ToString: string;
  11. begin
  12.   case Self of
  13.     mtFirstType  : Result := 'My First Type';
  14.     mtSecondType : Result := 'My Second Type';
  15.   end;
  16. end;

Documentation about Helper Types:
https://wiki.freepascal.org/Helper_types
« Last Edit: September 30, 2019, 05:15:42 pm by Handoko »

simone

  • Hero Member
  • *****
  • Posts: 573
Re: Type to String
« Reply #3 on: September 30, 2019, 06:29:05 pm »
Another way is to use a sort of associative array, as in the following example:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$MODESWITCH TYPEHELPERS}
  3.  
  4. type
  5.   TMyType = (mtFirstType, mtSecondType);
  6.   TMyTypeHelper = type Helper for TMyType
  7.   public
  8.     function ToString: string;
  9.   end;
  10.  
  11. var
  12.   T : TMyType;
  13.  
  14. function TMyTypeHelper.ToString: string;
  15. var
  16.   Conv : array [TMyType] of string=('mtFirstType', 'mtSecondType');
  17. begin
  18.   Result:=Conv[Self];
  19. end;
  20.  
  21. begin
  22.   T:=mtFirstType;
  23.   writeln(T.ToString);
  24.   readln;
  25. end.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

bdexterholland

  • Jr. Member
  • **
  • Posts: 65
  • uh?
Re: Type to String
« Reply #4 on: September 30, 2019, 11:28:28 pm »
Thanks guys!

Type helper done the work!
[sleep .....]

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: [Solved] Type to String
« Reply #5 on: September 30, 2019, 11:56:26 pm »
For future references, Enums can be translated directly to text..
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23.   Ttest = (One, two, three,Etc);
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.Button1Click(Sender: TObject);
  35. Var
  36.   S:String;
  37. begin
  38.    WriteStr(S, TTest(3)); //Generate a string of the third index.
  39.    Caption := S;
  40. end;
  41.  
  42. end.
  43.  
  44.  
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: [Solved] Type to String
« Reply #6 on: October 01, 2019, 09:05:29 am »
Yes, the solution by JdeHaan, hence I removed my duplicate entry. Credit is where credit is due.
« Last Edit: October 01, 2019, 09:10:36 am by Thaddy »
Specialize a type, not a var.

simone

  • Hero Member
  • *****
  • Posts: 573
Re: [Solved] Type to String
« Reply #7 on: October 01, 2019, 09:13:31 am »
The use of writestr is certainly the best solution.  I note only that with this approach the string obtained from the conversion is perfectly identical to the corresponding identifier in the enumeration.  If you want a different string, I think you should use the solution proposed by me or Handoko.
« Last Edit: October 01, 2019, 10:36:24 am by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: [Solved] Type to String
« Reply #8 on: October 01, 2019, 09:33:04 am »
Yes.
Specialize a type, not a var.

 

TinyPortal © 2005-2018