Solved

Untyped var parameters

Posted on 1998-05-15
8
510 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 

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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can Viruses spread while transferring Binary data with Winsock 2 105
oracle global variables 4 80
Dev express lookupcombo 3 64
Delphi Firemonkey send email on Android 1 76
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 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