Solved

Can't create a default value for a PChar parameter

Posted on 2006-11-15
3
694 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

773 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