Solved

Can't create a default value for a PChar parameter

Posted on 2006-11-15
3
684 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

9 Experts available now in Live!

Get 1:1 Help Now