?
Solved

Untyped var parameters

Posted on 1998-05-15
8
Medium Priority
?
521 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 100 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month12 days, 8 hours left to enroll

777 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