Solved

Can't create a default value for a PChar parameter

Posted on 2006-11-15
3
704 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
[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
  • 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

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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