Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delphi record to string conversion example, using operator overload. #delphi
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TLocString = record
Key, Value: string;
class operator Implicit(a: TLocString): string;
class function CreateNew(const aKey, aValue: string): TLocString; static;
end;
class function TLocString.CreateNew(const aKey, aValue: string): TLocString;
begin
Result.Key := aKey;
Result.Value := aValue;
end;
class operator TLocString.Implicit(a: TLocString): string;
begin
Result := a.Value;
end;
var
tmpLocString: TLocString;
s: string;
begin
try
tmpLocString := TLocString.CreateNew('key123', 'value123');
s := tmpLocString;
Writeln(s);
Readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.