?
Solved

Can't create a default value for a PChar parameter

Posted on 2006-11-15
3
Medium Priority
?
718 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 750 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

839 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