Recent

Author Topic: (SOLVED) From TFieldType to string and the other way around  (Read 5227 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
(SOLVED) From TFieldType to string and the other way around
« on: October 29, 2019, 12:23:24 pm »
Hi guys, I don't remember how to turn a TFieldType (example ftString, ftSmallInt, etc.) into string and the other way around. What is the syntax already?
« Last Edit: October 29, 2019, 01:48:41 pm by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: From TFieldType to string and the other way around
« Reply #1 on: October 29, 2019, 12:46:29 pm »
Code: Pascal  [Select][+][-]
  1. uses db;
  2.  
  3. function FieldTypeToStr(aFieldType: TFieldType): String;
  4. begin
  5.   Exit(Fieldtypenames[aFieldType]);
  6. end;
  7.  
  8. function StrToFieldType(aString: String): TFieldType;
  9. var
  10.   ft: TFieldType;
  11. begin
  12.   aString := LowerCase(aString);
  13.   Delete(aString, 1, 2);
  14.   for ft := Low(TFieldType) to High(TFieldType) do
  15.     if aString = LowerCase(Fieldtypenames[ft]) then
  16.       Exit(ft);
  17.   Result := ftUnknown;
  18. end;

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: From TFieldType to string and the other way around
« Reply #2 on: October 29, 2019, 01:48:30 pm »
Thanks
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

wp

  • Hero Member
  • *****
  • Posts: 13431
Re: (SOLVED) From TFieldType to string and the other way around
« Reply #3 on: October 29, 2019, 01:54:42 pm »
Like for any enumeration you can use GetEnumName and GetEnumValue from unit typinfo:

Code: Pascal  [Select][+][-]
  1. uses
  2.   db, typinfo;
  3.  
  4. function FieldTypeToString(AFieldType: TFieldType): String;
  5. begin
  6.   Result := GetEnumName(Typeinfo(TFieldType), integer(AFieldType));
  7. end;
  8.  
  9. function StringToFieldType(Str: String): TFieldType;
  10. begin
  11.   Result := TFieldType(GetEnumValue(TypeInfo(TFieldType), str));
  12. end;

If you don't want the "ft" in the converted strings you must remove them explicitly like in Howard's example.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: (SOLVED) From TFieldType to string and the other way around
« Reply #4 on: October 29, 2019, 02:32:37 pm »
wp is helpful in drawing attention to the typinfo routines, which are applicable to any enumeration.
The drawback of a one-line StringToFieldType() routine relying on a typecast of GetEnumValue is that it is not very robust.
For instance
Code: Pascal  [Select][+][-]
  1. var
  2.   ft: TFieldType;
  3. begin
  4.   ft := StringToFieldType('ftInvalidTypeString');
  5.   ..  
will lead to a runtime error, or perhaps a crash.
Sure it is programmer error, but "we all make many mistakes", as James once wrote.



wp

  • Hero Member
  • *****
  • Posts: 13431
Re: (SOLVED) From TFieldType to string and the other way around
« Reply #5 on: October 29, 2019, 05:19:09 pm »
Of course this can be added:
Code: Pascal  [Select][+][-]
  1. function StringToFieldType(Str: String): TFieldType;
  2. var
  3.   ft: Integer;
  4. begin
  5.   ft := GetEnumValue(TypeInfo(TFieldType), str);
  6.   if (ft >= ord(Low(TFieldType))) and (ft <= ord(High(TFieldType))) then
  7.     Result := TFieldType(ft)
  8.   else
  9.     Result := ftUnknown;
  10.     // or: raise Exception.Create('Unknown field type');
  11. end;

 

TinyPortal © 2005-2018