0

I need to pass a pair of value in array parameter function.

It is possible to call this form?

Call

Resul:= Validate( ['Johni', 18], ['Douglas', 22], ['Marangon', 19], [Dani, 29] )

Implementation

function Validate( /* Here, include pair array parameter */ ): Boolean
begin
  // Implemetation  
end;

The solution find.

TData = record
  Name: string;
  Age: Integer; 
  cosntructor Add( const AName: string; const AAge: Integer );
end;

cosntructor TData.Add( const AName: string; const AAge: Integer );
begin
  Name:= AName
  Age:= AAge;
end;

function Validate( const Array of TData ): Booelan;
begin
  // implemtation
end;

Result:= Validate( [ TData.Add( 'Johni', 18 ), TData.Add('Douglas', 22), TData.Add('Marangon', 19) TData.Add(Dani, 29) ] );

Thank you.

1
  • The example you have provided TData.Add is the best that can be achieved. Though I would prefer the name Init for the method. If using an older version of Delphi that does not support record methods a simple: function InitTData(const AName: string; const AAge: Integer): TData works just as well. Commented Jan 30, 2014 at 15:12

1 Answer 1

2

No, it's not possible to use array notation to construct records. Array notation only constructs arrays and sets. Instead, you might take inspiration from the Point or Rect functions and make a standalone function that builds TData instances:

function Data(const Name: string; Age: Integer): TData;
begin
  Result.Name := Name;
  Result.Age := Age;
end;

It would give you more concise notation than calling methods of the TData type:

Validate([Data('Johni', 18), Data('Douglas', 22), Data('Marangon', 19),
          Data(Dani, 29)]);
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.