Jump to content
PenelopeSkye

Query result to dynamic array

Recommended Posts

11 minutes ago, skyzoframe[hun] said:

there are the opportunity to create redundancy.

look, I think that this explains more than words

type
  TMyRec = record
    Id: integer; // can generate random values, yes! but does matter in function below!
    Name: string;
  end;

function FuncReturnArray(AddRecordOnArray: boolean): TArray<TMyRec>; // none random value is returned
var
  LRecX: TMyRec;
begin
  // the "result" will be "default" = [], does matter if you the content can have random values, when call this function!
  // There is not content! so, none "record" can be accessed
  // result := []; // if you want do it!
  // ...
  if AddRecordOnArray then
    begin
      LRecX.Id   := 1;
      LRecX.Name := 'hello1';
      result     := result + [LRecX];
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  LReturn : TArray<TMyRec>;
  LRecTest: TMyRec;
begin
  LRecTest.Id   := 123;
  LRecTest.Name := 'hello world';
  //
  // FuncReturnArray(true) := LRecTest; // it's not possible!
  //
  LReturn := FuncReturnArray(false {true});
  //
  for var R in LReturn do
    ShowMessage(R.Id.ToString + ', ' + R.Name);
  //
  ShowMessage(length(LReturn).ToString);
  //
  // if dont exists any record on array, for sure you'll a exception, not?
  if length(LReturn) > 0 then
    ShowMessage(LReturn[0].Id.ToString + ', ' + LReturn[0].Name);
end;

 

Share this post


Link to post

Yes , you are right.

38 minutes ago, programmerdelphi2k said:

Definitely not!
because the expected target (function return) is an "Array", so if the array is empty, there is nothing to expect from its "possible" content (the records), and if there is "content", there will be "records" with values!

 

Share this post


Link to post

Hi James, our company has paid to have our code converted to c#, so I won't be posting here anymore.  I just wanted to thank you for all your help and support.  If there is any feedback I can leave anywhere please let me know, but otherwise I am taking it all as a big personal favor!!! :classic_laugh:

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×