IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

January 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

KB101712



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:
Const 
  kFeetToMeter: Double = 3.2808;
  kMeterToFeet: Double = .3048;
  kName: String = "Mike";
 
//Local constants:
procedure SomeProcedure;
const
  kPI: Double=3.1459;
begin
end;

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:

{$J+}
const
   clicks : Integer = 1; //not a true constant
{$J-}
begin
  Form2.Caption := IntToStr(clicks) ;
  clicks := clicks + 1;
end;

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).
//var
//  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).
//var
//  ButtonClicks: Integer = 0;
//Does not compile!
//3. Writable typed constant is global
//   (same as more correct #3 above, but weird).

{$J+}
const
  ButtonClicks: Integer = 0;
{$J-}
begin
  If ButtonClicks >= 5 then
    ShowMessage('Stop clicking the button.')
  Else
  begin
    ButtonClicks := ButtonClicks + 1;
    Form2.Caption := IntToStr(ButtonClicks);
  end;
end;

More Info

Definition:  Computer Language Constants

Comments

0 Comments.
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 = P1197A1
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.

Beginner

1 Beginner Level Question

Question #1: Multiple Choice

Which example uses the correct syntax to declare a constant?

Answer:
1. 

const
  kPI : Double := 3.1459;

2. 


const
  kPI Double := 3.1459;


3. 


const
  kPI Double = 3.1459;


4. 

const
  kPI As Double := 3.1459;


5. 

const
  kPI : Double = 3.1459;


 KB Article #101712 Counter
14839
Since 12/17/2008
-
   Contact Us!
 
Have a question? Need our services? Contact us now.
--Mike Prestwood

Call: 916-726-5675

email: info@prestwood.com


Go ahead!   Use Us! Call: 916-726-5675 


©1995-2019 Prestwood IT Solutions.   [Security & Privacy]