Solved

Untyped var parameters

Posted on 1998-05-15
8
499 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
Comment Utility
Hi skanade,

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

ZiF.
0
 

Author Comment

by:skanade
Comment Utility
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
Comment Utility
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
 

Author Comment

by:skanade
Comment Utility
>>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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:skanade
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks d003303, now I understand it.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now