Einzelnen Beitrag anzeigen

BadenPower

Registriert seit: 17. Jun 2009
616 Beiträge
 
#9

AW: Delphifunktion um die Länge eines Array in einem "Array of Variant" zu bestimmen?

  Alt 21. Jan 2015, 21:29
hier einmal ein wenig Testcode:

benötigt
1x Form
1x Button
4x Label

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;


type
  TMyVarArray = Array of Variant;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}



function LocalNewArray(): TMyVarArray;
begin
  //Result := VarArrayCreate([-3,-2],varVariant);
  //Result := VarArrayCreate([0,5],varVariant);
  SetLength(Result,0);


end;

procedure TForm1.Button1Click(Sender: TObject);
var
  lArray1: Array of Variant;
  lArray2: Array of Variant;
begin

  lArray1 := VarArrayCreate([0,5],varVariant);
  //SetLength(lArray1[0],0);
  lArray1[0] := VarArrayCreate([-3,-2],varVariant);
  lArray1[0][-2] := 'Test';

  lArray2 := VarArrayCreate([0,10],varVariant);
  lArray2[0] := VarArrayCreate([0,10],varVariant);
  //lArray2[0] := LocalNewArray();
  //SetLength(lArray2[0],0);

  try
    Label1.Caption := lArray1[0][-2];
    //Label1.Caption := IntToStr(Length(lArray1));
  except
    Label1.Caption := 'Fehler';
  end;

  try
    Label2.Caption := IntToStr(Length(lArray2));
  except
    Label2.Caption := 'Fehler';
  end;

  try
    //Label3.Caption := IntToStr(VarArrayHighBound(lArray1[0],1));
    Label3.Caption := IntToStr(VarArrayDimCount(lArray1[0]));
    //Label3.Caption := IntToStr(Length(lArray1[0]));
    //Label3.Caption := IntToStr(High(lArray1[0]));
  except
    Label3.Caption := 'Fehler';
  end;

  try
    //Label4.Caption := IntToStr(VarArrayLowBound(lArray2,2));
    //Label4.Caption := IntToStr(VarArrayHighBound(lArray2,2));
    //Label4.Caption := IntToStr(VarArrayLowBound(lArray2[0],1));
    //Label4.Caption := IntToStr(VarArrayHighBound(lArray2[0],1));
    //Label4.Caption := IntToStr(VarArrayDimCount(lArray2[0]));
    Label4.Caption := IntToStr(Length(lArray2[0]));
    //Label4.Caption := IntToStr(High(lArray2[0]));
  except
    Label4.Caption := 'Fehler';
  end;



end;

end.
Programmieren ist die Kunst aus Nullen und Einsen etwas sinnvollen zu gestalten.
Der bessere Künstler ist allerdings der Anwender, denn dieser findet Fehler, welche sich der Programmierer nicht vorstellen konnte.
  Mit Zitat antworten Zitat