Einzelnen Beitrag anzeigen

Medium

Registriert seit: 23. Jan 2008
3.679 Beiträge
 
Delphi 2007 Enterprise
 
#8

AW: Pointer auf Array

  Alt 9. Mai 2012, 13:06
Alternativ ließe sich auch noch eine Klasse um den Arraytypen bauen, die dann dafür sorgt, dass der RefCounter nicht versickert.
Delphi-Quellcode:
type
  TByteArray = array of Byte;
  TMyArrayContainer = class
  private
    FData: TByteArray;
    function GetValue(index: Integer): Byte;
    procedure SetValue(index: Integer; Value: Byte);
    function GetLength: Integer
    procedure SetLength(Value: Integer);
  public
    property Data: TByteArray read FData write FData;
    property Value[index: Integer]: Byte read GetValue write SetValue; default;
    property Length: Integer read GetLength write SetLength;
  end;

function TMyArrayContainer.GetValue(index: Integer): Byte;
begin
  if (index<Low(FData)) or (index>High(FData)) then
    raise EIndexOutOfBoundsException.Create
  else
    result := FData[index];
end;

procedure TMyArrayContainer.SetValue(index: Integer; Value: Byte);
begin
  if (index<Low(FData)) or (index>High(FData)) then
    raise EIndexOutOfBoundsException.Create
  else
    FData[index] := Value;
end;

procedure TMyArrayContainer.SetLength(Value: Integer);
begin
  SetLength(FData, Value);
end;

function TMyArrayContainer.GetLength: Integer;
begin
  result := Length(FData);
end;
Das noch mit Generics gibt einen ziemlich netten OOP-mäßigen Wrapper, der auch insgesamt das Handling etwas aufhübscht wie ich finde.
"When one person suffers from a delusion, it is called insanity. When a million people suffer from a delusion, it is called religion." (Richard Dawkins)
  Mit Zitat antworten Zitat