Last active
December 24, 2015 17:09
-
-
Save tdelphi/6833017 to your computer and use it in GitHub Desktop.
Delphi record to string conversion example, using operator overload. #delphi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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