Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
Delphi 10 Seattle Enterprise
|
AW: array als var-Parameter
30. Jun 2015, 11:53
Dann bau dir halt einen Wrapper
Delphi-Quellcode:
type
TPIntArray = array of PInteger;
TIntValueSetter = record
private
FValues: TPIntArray;
public
function AddVar( var Value: Integer ): TIntValueSetter;
procedure SetValue( Value: Integer );
end;
TValueSetter = record
class function Int: TIntValueSetter; static;
end;
{ TValueSetter }
class function TValueSetter.Int: TIntValueSetter;
begin
SetLength( Result.FValues, 0 );
end;
{ TIntValueSetter }
function TIntValueSetter.AddVar( var Value: Integer ): TIntValueSetter;
var
LValues: TPIntArray;
LIdx: Integer;
begin
SetLength( LValues, Length( FValues ) + 1 );
for LIdx := low( FValues ) to high( FValues ) do
LValues[ LIdx ] := FValues[ LIdx ];
LValues[ high( LValues ) ] := @Value;
Result.FValues := LValues;
end;
procedure TIntValueSetter.SetValue( Value: Integer );
var
LIdx: Integer;
begin
for LIdx := low( FValues ) to high( FValues ) do
FValues[ LIdx ]^ := Value;
end;
procedure SetValue( Values: array of Integer; Value: Integer = 0 );
var
LIdx: Integer;
begin
for LIdx := low( Values ) to high( Values ) do
Values[ LIdx ] := Value;
end;
procedure Test;
var
Val1, Val2, Val3: Integer;
TargetValue: Integer;
Set1, Set2, Set3: TIntValueSetter;
begin
Val1 := 42;
Val2 := 84;
Val3 := 34;
TargetValue := 0;
TValueSetter.Int.AddVar( Val1 ).AddVar( Val2 ).AddVar( Val3 ).SetValue( TargetValue );
Assert( ( Val1 = TargetValue ) and ( Val2 = TargetValue ), 'Mist, hat nicht funktioniert' );
Set1 := TValueSetter.Int.AddVar( Val1 ).AddVar( Val2 );
Set2 := TValueSetter.Int.AddVar( Val2 ).AddVar( Val3 );
Set3 := TValueSetter.Int.AddVar( Val1 ).AddVar( Val2 ).AddVar( Val3 );
WriteLn( 'Set1.SetValue( 1 )' );
Set1.SetValue( 1 );
WriteLn( 'Val1: ', Val1, ' Val2: ', Val2, ' Val3: ', Val3 );
WriteLn;
WriteLn( 'Set2.SetValue( 2 )' );
Set2.SetValue( 2 );
WriteLn( 'Val1: ', Val1, ' Val2: ', Val2, ' Val3: ', Val3 );
WriteLn;
WriteLn( 'Set3.SetValue( 3 )' );
Set3.SetValue( 3 );
WriteLn( 'Val1: ', Val1, ' Val2: ', Val2, ' Val3: ', Val3 );
end;
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
|
|
Zitat
|