I.T. Discussion Community!
-Collapse +Expand
Search Delphi Group:

-Collapse +Expand Delphi To/From
-Collapse +Expand Delphi Store

Prestwood eMagazine

February Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Language Basics   Print This     
  From the September 2015 Issue of Prestwood eMag
Delphi Language Basics:
Delphi Constants (Const kPI: Double=3.1459;)
Posted 11 years ago on 12/17/2008 and updated 1/28/2009
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page
 Tags: Delphi , Constants


General Info: Computer Language Constants

A constant is just like a variable (it holds a value) but, unlike a variable, you cannot change the value of a constant.

Delphi Constants

In Delphi, you define constants similar to how you define variables but use the Const keyword instead of the Var keyword. Declare global constants in a unit's interface section and unit constants (scope limited to unit) in the implementation section. Declare local constants above the begin..end block.

Syntax Example:
  kFeetToMeter: Double = 3.2808;
  kMeterToFeet: Double = .3048;
  kName: String = "Mike";
//Local constants:
procedure SomeProcedure;
  kPI: Double=3.1459;

Writable Typed Constants

Delphi also supports writable typed constants which are not constants. They are initialized global variables and are a hold-over from earlier versions of Delphi and Turbo Pascal. They are turned off by default in later versions of Delphi so to use them you have to use the compiler directive {$J+} to enable then {$J-} to disable.

Writable Typed Constants Example:

   clicks : Integer = 1; //not a true constant
  Form2.Caption := IntToStr(clicks) ;
  clicks := clicks + 1;

Although the Delphi help says not to use writable typed constants, you may find them useful when you wish to use static local variables. You'll have to decide if you wish to use them. The benefit #3 below has over the suggested #1 is that #3 is local and you therefore don't  have to worry about duplicate constant/variable names; otherwise, they both work well.

//1. Global variable (suggested technique).
//  ButtonClicks: Integer = 0;

procedure TForm2.Button5Click(Sender: TObject);
//2. Does not work (local variable is destroyed with each click
//   so the compiler doesn't even allow this to compile).
//  ButtonClicks: Integer = 0;
//Does not compile!
//3. Writable typed constant is global
//   (same as more correct #3 above, but weird).

  ButtonClicks: Integer = 0;
  If ButtonClicks >= 5 then
    ShowMessage('Stop clicking the button.')
    ButtonClicks := ButtonClicks + 1;
    Form2.Caption := IntToStr(ButtonClicks);

More Info

Definition:  Computer Language Constants


Share a thought or comment...
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1204A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.


1 Beginner Level Question

Question #1: Multiple Choice

Which example uses the correct syntax to declare a constant?


  kPI : Double := 3.1459;


  kPI Double := 3.1459;


  kPI Double = 3.1459;


  kPI : Double = 3.1459;


  kPI As Double := 3.1459;

 KB Article #101712 Counter
Since 12/17/2008
   Contact Us!
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

2,018 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...

©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech