I've created a record which encapsulates scenarios of version numbers in an integer array and is interchangeable as a string...
type
TVersion = record
Values: array of Integer;
function Count: Integer;
class operator implicit(aVersion: TVersion): String;
class operator implicit(aVersion: String): TVersion;
end;
class operator TVersion.implicit(aVersion: TVersion): String;
var
X: Integer;
begin
Result:= '';
for X := 0 to Length(aVersion.Values)-1 do begin
if X > 0 then Result:= Result + '.';
Result:= Result + IntToStr(aVersion.Values[X]);
end;
end;
class operator TVersion.implicit(aVersion: String): TVersion;
var
S, T: String;
I: Integer;
begin
S:= aVersion + '.';
SetLength(Result.Values, 0);
while Length(S) > 0 do begin
I:= Pos('.', S);
T:= Copy(S, 1, I-1);
Delete(S, 1, I);
SetLength(Result.Values, Length(Result.Values)+1);
Result.Values[Length(Result.Values)-1]:= StrToIntDef(T, 0);
end;
end;
function TVersion.Count: Integer;
begin
Result:= Length(Values);
end;
Usage:
var
V: TVersion;
begin
V:= '1.2.3.4';
V.Values[3]:= 400;
ShowMessage(V);
SetLength(V.Values, 2);
V.Values[0]:= 7;
V.Values[1]:= 0;
ShowMessage(V);
end;
What I would like to do is be able to compare these versions using standard operators, for example...
if V1 > V2 then ShowMessage('Version 1 is newer than version 2')
else if V1 < V2 then ShowMessage('Version 1 is older than version 2')
else if V1 = V2 then SHowMessage('Version 1 and version 2 are the same');
How can I do this, if possible?