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

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

Prestwood eMagazine

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

   ► KBProgrammingDelphi for W...Language Basics   Print This     
  From the August 2015 Issue of Prestwood eMag
 
Delphi Language Basics:
Delphi Variables (var x: Integer = 0;)
 
Posted 11 years ago on 11/7/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 , Variables

KB101567



Languages Focus: Variables

A variable holds a value that you can use and change throughout your code so long as the variable is within scope. With variable declaration, you not only want to know the syntax of how you declare a variable but you also want to know where. Are you allowed to declare a variable inline? What are the available scopes: local vs. global. Can you assign a value at the same time you declare a variable?

Delphi Variables

The Delphi language is a strongly typed language so you have to specifically declare variables and frequently use commands such as IntToStr and StrToInt.

Declare global variables in the interface section of a unit, variables declared within the implementation section (but not within a method) have a scope limited to the unit. You declare local variables in a var block outside (above) your begin..end code block. You cannot declare variables in-line (inside begin..end).

You can initialize global and unit variables but you cannot initialize local variables.

Delphi offers many variable types. Some common variable types include String, WideString, PCharInteger, Boolean, Single, Double, Pointer, and Variant.

Note: D2009 introduced a new UnicodeString type.

Syntax Example:
procedure SomeProcedure;
var
  Fullname: String;
  Age: Integer;
  X, Y, Z: Double;
  MyArray: array [1..100] of Char;
begin
end;
 
//You can initialize global variables.
var
  ClickCounter: Integer = 0;

Interface versus Implementation

Variables declared in the interface section of a unit are truly global and you should limit the number of variables you declare in the interface section especially for reusable units (units that contain classes). Variables declared in the implementation section of a unit have a scope limited to the unit.

Initializing Local Variables

You cannot initialize local variables. The following commented out code, does not work:

procedure TForm2.Button6Click(Sender: TObject);
var
//  ButtonClicks: Integer = 0;  //Does not work!
begin
end;

Initializing Global Variables

You can initialize global variables but not local variables. Suppose you wish to allow a user to click a button up to 3 times. You can initialize a global variable to track clicks.

The following code does work:

var
ClickCounter: Integer = 0; //Does work!
procedure TForm2.Button7Click(Sender: TObject);
begin
If ButtonClicks >= 3 then
ShowMessage('Stop clicking the button.')
Else
begin
ButtonClicks := ButtonClicks + 1;
Form2.Caption := IntToStr(ButtonClicks);
end;
end;

Instance Counter

Initialized Global Variables and Static Data: Initialized global variables are important for many reasons but I will discuss it's relation to static class data here. Static class data is data of a class that retains state (it's value) whether or not there is an instance of a class. Suppose you wish to have an instance counter. If no classes are currently created, the current value of your instance counter needs to be 0. For each class created, you add 1. For each destroyed, you subtract 1.

More Info

KB Post:  Delphi Instance Counter

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 = P1193A1
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

The correct syntax for a declaritive variable assignment is?

Answer:
1. 
Dim Married As String = "N"
2. 
Married String := 'N';
3. 
Married : String := 'N';
4. 
String Married = "N";
5. 
Married : String = 'N';

 KB Article #101567 Counter
23039
Since 11/7/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,259 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