[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 560
  • Last Modified:

Untyped var parameters

A function declares a formal parameter as untyped var:
  function Read(var buffer; ...)

If I pass a string variable to it, it doesn't work. I even tried presetting SetLength. What is the correct way to pass a string to the above paramter?

Thanks,
Sanjay
0
skanade
Asked:
skanade
1 Solution
 
ZifNabCommented:
Hi skanade,

I don't understand you correclty : shouldn't it be :  function Read(var buffer: string; ...)

ZiF.
0
 
skanadeAuthor Commented:
Hi ZiF,

That's what an 'untyped' parameter is. There is no type spec. Here is an example of BlockRead from Delphi:

procedure BlockRead(var F: File; var Buf; Count: Integer [; var AmtTransferred: Integer]);

The second parameter is untyped. That's where a string passed doesn't seem to work. I tried all sorts of conversions, PChar, Pointer, etc.

Sanjay
0
 
kjtengCommented:
Did you type cast the untyped parameter in the function?

Here is an example:

procedure test( var src);
begin
  showmessage( string( src));
end;

IMHO you should try to use the Type-Safe Open Arrays
(array of const) as parameter because it is safer.

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
skanadeAuthor Commented:
>>Did you type cast the untyped parameter in the function?

It's not my own function so I have no control. I am trying to use BlockWrite from the library which uses the untypted parameter. Please see BlockWrite from Delphi help.

I have tried sending PChar typecase and even tried SetLength before sending. But the value of the string never reaches the file. If I send from a zero-based array of characters, it works. What I want is to somehow get strings to it without allocating arrays of huge length.

Sanjay
0
 
skanadeAuthor Commented:
Anyway, I found the answer on my own after some experimentation. I need to pass it as
   PChar(mystr)^

to the BlockWrite function's buffer untyped parameter. For BlockRead, I am reading it into dynamically allocated memory in a PChar pointer by P^.

Regards,
Sanjay
0
 
ZifNabCommented:
Hi all,
 
didn't know about this feature, never needed such a thing and then again, why not using a variant type?

c.U. Zif.
0
 
d003303Commented:
Hi Zif,
a variant is a high-level type that can be any type, but also stores its type information. An untyped parameter is low-level, in fact just a pointer to the memory a variable allocates. Thus, PChar(MyString)^ does the trick.

Slash/d003303
0
 
ZifNabCommented:
Thanks d003303, now I understand it.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now