0

So I have a function that takes a dataset and needs to return all values from a field. I can't figure out how to make the function return an array, as when I declare an array I keep get the error

[DCC Error] dataModuleImportMonetar.pas(20): E2003 Undeclared identifier: 'nr'

Here is my code

unit dataModuleImportMonetar;

interface

uses
  SysUtils, Classes, DB, ADODB;

type
  TDataModule2 = class(TDataModule)
    conn: TADOConnection;
    importMonetar: TADOStoredProc;

    qMonetare: TADOQuery;
  private
    { Private declarations }
    //nr  : Array[Word] of Integer;
    nr : IntegerArray;
  public
    { Public declarations }
    function nrFisiere ( dataSet : TDataSet) : nr ;
  end;

var
  DataModule2: TDataModule2;

implementation

{$R *.dfm}

{ TDataModule2 }

function TDataModule2.nrFisiere(dataSet: TDataSet): nr;
 var
  i : Integer;
begin
       dataSet.First;

       for i := 0 to dataSet.RecordCount  do
         begin
          Return nr[i] := dataSet.FieldByName('nrMonetare').Value;
         end;

end;


end.

What Am I doing wrong? How cand I declare a function that will return an array of Integer

For @TLama

If I do that ( already did before asking )

 type
  TDataModule2 = class(TDataModule)
    conn: TADOConnection;
    importMonetar: TADOStoredProc;
    TIntArray : array of Integer;
    qMonetare: TADOQuery; 

I get an othe error [DCC Error] dataModuleImportMonetar.pas(12): E2217 Published field 'TIntArray' not a class or interface type

11
  • You must define a type, not declare a variable. Commented Jul 16, 2014 at 9:09
  • Can you be more specific, I don't follow... Commented Jul 16, 2014 at 9:10
  • 1
    Using a field of type IntegerArray is a very bad idea as it is declared as IntegerArray = array[0..$effffff] of Integer;. Thus the field nr will occupy about 1GB of memory. Commented Jul 16, 2014 at 9:19
  • 1
    TLama showed you the declaration of a dynamic array. In addition the use of Return inside the function reveals that you seem to be not very familiar with the Delphi syntax. I assume a basic course - be it a book or from a tutor. Commented Jul 16, 2014 at 9:24
  • 1
    @TLama your paste-bin link has a typo. You declared the type with : instead of =. Commented Jul 16, 2014 at 10:01

1 Answer 1

6

You should use TIntegerDynArray from Types.pas. If you are using a Delphi Version >= 2010 you could also use TArray<Integer>.

unit dataModuleImportMonetar;

interface

uses
  SysUtils, Classes, DB, ADODB, Types;

type
  TDataModule2 = class(TDataModule)
    conn: TADOConnection;
    importMonetar: TADOStoredProc;

    qMonetare: TADOQuery;
  public
    function nrFisiere(dataSet: TDataSet): TIntegerDynArray;
  end;

var
  DataModule2: TDataModule2;

implementation

{$R *.dfm}

{ TDataModule2 }

function TDataModule2.nrFisiere(dataSet: TDataSet): TIntegerDynArray;
var
  i: Integer;
  f: TField;
begin
  dataSet.First;
  SetLength(Result, dataSet.RecordCount);
  i := 0;
  // remember the field to avoid calling FieldByName in a loop
  f := dataSet.FieldByName('nrMonetare'); 
  while not dataSet.Eof do
  begin
    Result[i] := f.Value;
    Inc(i);
    dataSet.Next;
  end;
end;

end.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.