Solved

Can't create a default value for a PChar parameter

Posted on 2006-11-15
3
690 Views
Last Modified: 2010-08-05
Here's something simple that won't compile:

const
  DEFAULT: PChar = 'DEFAULT';

procedure MyProc(FirstParam: PChar = DEFAULT; SecondParam: PChar = DEFAULT);

implementation

procedure MyProc(FirstParam: PChar = DEFAULT; SecondParam: PChar = DEFAULT);
begin
end;

The compile error is "Constant expression expected", and hits on the definition of the MyProc procedure.  The problem is that it's a PChar parameter type.  If I change the parameter and constant types to strings, it compiles.  If I change the DEFAULT constant value to a nil, it also compiles.  I've tried a number of different ways to define the constant, but nothing seems to work.  Changing PChar to a string is not an option.  I get the same error in both Delphi 6 and Delphi 2006.  I need this to work in Delphi 2006.  Please help.
0
Comment
Question by:Meldrachaun
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
rowdy_h earned 250 total points
ID: 17949426
I think the problem is probably that the pointer is fundamentaly not constant even though what it points to is.
Taken from the Delphi help
"A constant expression is an expression that the compiler can evaluate without executing the program in which it occurs. Constant expressions include numerals; character strings; true constants; values of enumerated types; the special constants True, False, and nil; and expressions built exclusively from these elements with operators, typecasts, and set constructors. Constant expressions cannot include variables, pointers, or function calls, except calls to the following predefined functions:
AbsChrHi      HighLengthLo      LowOddOrd      PredRoundSizeOf      SuccSwapTrunc"
As the compiler has to execute the program to get the memory address the pointer points to it cannot be a constant.

As an alternative could you not instead do

const
  DEFAULT: PChar = 'DEFAULT';

procedure MyProc(FirstParam: PChar; SecondParam: PChar);

implementation

procedure MyProc(FirstParam: PChar; SecondParam: PChar);
begin
  if not Assigned(FirstParam) then
    FirstParam := DEFAULT;
  if not Assigned(SecondParam) then
    SecondParam := DEFAULT;
end;
0
 
LVL 4

Author Comment

by:Meldrachaun
ID: 17949651
Ahhh. that explains it.  That get's me closer, but your solution doesn't let me leave the the parameters empty when calling the proc.  The default constant doesn't have to be the text 'DEFAULT', it can be anything I want, but it can't be nil, so based on your info, I tried this:

const
  DEFAULT: PChar = PChar(10);

procedure MyProc(FirstParam: PChar = DEFAULT; SecondParam: PChar = DEFAULT);

That still didn't compile.  However,interestingly enough, this did work:

procedure MyProc(FirstParam: PChar = PChar(10); SecondParam: PChar = PChar(10));

Which I find really wierd.  It works, and it allows me do all three of these kinds of calls:

  MyProc;
  MyProc('foo');
  MyProc(DEFAULT, 'foo');

However, I would really like to use the DEFAULT constant in the procedure definition, instead of something like PChar(10).  This code will be used by other developers, and it won't be obvious to use the DEFAULT constant in cases like MyProc(DEFAULT, 'foo');  Any ideas?

0
 
LVL 4

Author Comment

by:Meldrachaun
ID: 17949872
I just figured it out.  If I change DEFAULT: PChar = PChar(10) to DEFAULT = PChar(10) it works.  Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
A short film showing how OnPage and Connectwise integration works.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

919 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

17 Experts available now in Live!

Get 1:1 Help Now