5

I have a Dynamc array of Records and I wish to pass one of the items in the array to a function by reference

So for example one of the array items - AArray[1].arecorditem is a string of 6 characters String[6]

the function would be -

function dosomething(var AStringVal:string):Integer;

So I would execute

Aresult:= dosomething(AArray[1].arecorditem);

however when I try to compile I get the Error Type of actual and formal var parameters must be identical.

Is this possible to do or should I assign the array item to a string and then pass the string to the function.

Thanks

Colin

4 Answers 4

5

Your question title and the actual question are not the same, so I'll give you an overview of both subjects.

You need to define an Array Type

TMyRecord = record
  Field1: String
  Field2: String
end;

TMyRecordArray = Array of TMyRecord

function DoSomething(const ARecordArray: TMyRecordArray): Integer;

This is if you want to pass an entire dynamic array of items to the function. If you just want to pass one item, you'd define the function like this:

function DoSomething(const ARecord: TMyRecord): Integer;

Now, if you want to pass the value of Field1 to the function, you would have to define the function as:

function DoSomething(const AField: String): Integer;

You cannot define the parameter as varor you'll end up with the error you're getting!

Additional:

As others have been saying, if you're using a fixed-length String for the field, you need to define it as a Type just as I have demonstrated above for TMyRecordArray.

TString6 = String[6];

Use that Type both for your Field, and your function Parameter.

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

Comments

4

You'll have to create a type:

    type
      TName = string[80];

So you can call your function this way:

    function doSomething(var name: TName): Integer;
    begin
        ...
        ...
        ...
    end;  

Working Example

program HelloWorld;

type
  TName = string[80];

type
  TCustomer = record
    name : Name;
    age : byte;
  end;

procedure qwerty(var name: TName);
begin
  name := 'doSomething';
end;

var
  customers : array[1..3] of TCustomer;
  b : Byte;
begin

  with customers[1] do
  begin
    name := 'qwerty';
    age := 17;
  end;

  with customers[2] do
  begin
    name := 'poiuy';
    age := 18;
  end;

  writeln(customers[1].name);

  qwerty(customers[1].name);
  writeln(customers[1].name);

  Readln(b);
end.

4 Comments

Try to use the unwritten conventions in formatting and syntax. Types have the T on the beginning of the word just like I have in my nick ;) Now you're mixing two things together, check the syntax Name: Name.
@TLama Are you a class or an instance? ;-)
@David, I'm just a type :) Sometimes like a class because I feel like I need more instances of me doing the same things on many places and sometimes like a declared boolean type, so it depends :)
@TLama I feel like a Thread, because I always need more than one of me performing the same task at the same time to achieve a quicker result!
1

String[6] is a ShortString with a maximum length of 255 characters. You need to either change the definition of doSomething to something like:

function dosomething(var AStringVal:string[6]):Integer;

or

  function dosomething(var AStringVal:ShortString):Integer;

or change the definition of the record so that arecorditem is of type String (as opposed to String[6]).

1 Comment

string[6] is not a valid type in a function/procedure declaration. Declare it outside, Type Str6 = string[6];
0

If you're not changing the value of the parameter in the function, you can change the variable type in the function declaration to const instead of var, the compiler will do the conversion for you:

Change from:

procedure doSomething(var AStringVal: String);

To this:

procedure doSomething(const AStringVal: String);

Now you can do this, without complaint:

var
  myShortString: String[6];
begin
  myShortString := '123456';
  doSomething(myShortString);
end;

When you pass variables by reference, they must be the same type. When you pass by other means, the compiler will try to convert for you.

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.