Solved

Untyped var parameters

Posted on 1998-05-15
8
507 Views
Last Modified: 2010-05-18
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
Comment
Question by:skanade
8 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1345869
Hi skanade,

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

ZiF.
0
 

Author Comment

by:skanade
ID: 1345870
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
 
LVL 2

Accepted Solution

by:
kjteng earned 50 total points
ID: 1345871
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:skanade
ID: 1345872
>>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
 

Author Comment

by:skanade
ID: 1345873
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1345874
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
 
LVL 4

Expert Comment

by:d003303
ID: 1345875
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1345876
Thanks d003303, now I understand it.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question