问题
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
回答1:
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.
回答2:
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.
回答3:
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]).
回答4:
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.
来源:https://stackoverflow.com/questions/8403578/delphi-passing-dynamic-array-of-records-to-function