Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Delphi Programming/Data types

From Wikibooks, open books for an open world
<Delphi Programming

First, let's define atype.

In Delphi, like inC,VB and unlikePHP,Python or other modern languages, a variable istyped and can only contain values of this type. To make it simple, a type is a variable attribute.

Basic types

[edit |edit source]

Delphi is extremely complete for this. There are lots of predefined types and you can also create your own types (we will see it later). Here is a table with the most useful basic types (also calledprimitive types):

TypeDescriptionSizeValue
booleanboolean1 bitTrue/False
charcharacter1 byteOne item of the ASCII table (or ANSI)
stringstring of charactersDepends on its number of charactersDynamic table of items of the ASCII table (or ANSI)
integerpositive or negative integer4 bytes-2147483648to 2147483647
cardinalunsigned integer4 bytes0to 4294967295
real (equivalent to double)real number8 bytes5.0 x 10^-324to 1.7 x 10^308
Remarks: Above are only the most popular predefined types. There are dozens of other types just for the integers (smallint, longint, int64...) whose usage fits very specific needs (like the size of the data we will put in) and so optimizes the memory usage. For more information about it, see the documentation in the Delphi help content.

char

[edit |edit source]

A char (a character) is something like '#', 'A', '2', ';', '.', '?' and so on. It has a number in a library of chars, the ASCII. In Delphi, a char is typed between two single quotes. If the given character is the single quote itself, you should double the character, in other words, you have to type four single quotes.

vara,b:char;begina:='H';// a = "H"b:='i';// b = "i"WriteLn(a);// Display "H"WriteLn(a,b);// Display "Hi"WriteLn(a,b,'!');// Display "Hi!"end.

string

[edit |edit source]

You have already worked with strings in the last chapter, such as 'Hello World' or 'Hi there' and so one. A string is a sequence of chars.

vartext1:string;character1:char;

And then write between begin and end:

character1:='H';text1:='ello World!';WriteLn(character1,text1);

If you want to write a single quote in a string, the best way is to type two successive single quotes:

text1:='I''m John Doe.';

Strings behave like the tables. You can access to a character using an index:

text:='Hello World!';WriteLn(text[1]);// Only print "H".

The first index is 1 (and not 0).

Textual expressions

[edit |edit source]

Strings can be linked using the + operator:

firstWord:='Hello';secondWord:='World';WriteLn(firstWord+' '+secondWord+'!');wholeText:=firstWord+' '+secondWord+'!';

The + operator can be used with variables with different types. According to the types, the result will have a different type:

  • string + string → string
  • string + char → string
  • char + char → string
  • char + string → string
  • string + number → error
  • number + string → error
  • number + number → number
  • number + char → error
  • char + number → error

Integers

[edit |edit source]

Integers are even integers, you write:

a:Integer;

Floats

[edit |edit source]

Floats are decimal numbers. They use afloating point. You can write:

c:Real;// Real is normally the same like Singlea:Single;b:Double;d:Extended;

An Extended is more precise than a Real, but needs more main storage.

Boolean

[edit |edit source]

Boolean is a logical value. It can be 'True' or 'False'. Write:

vara:Boolean;begina:=True;// ora:=False;

But there is only a sense of Boolean when you know the conditional structures in the next chapter.

Arrays

[edit |edit source]

An array is a collection of strings, floats, integers or any data type. It can also contain an array. In this case, we have an array with several dimensions. You say:

a:array[from..to]ofdata_type;a:array[1..3]ofstring;

for one dimension. For two:

a:array[fX..tX]ofarray[fY..tY]ofdata_type;

and so on. A single element of an array is for example:

a[1]:='Hello 'a[2]:='World'a[3]:='!';WriteLn(a[1],a[2],a[3]);

Record

[edit |edit source]

A record (also called astructure in other languages) represents a set of heterogeneous data. Each item is a field. The record type declaration specifies the name and the type of each field. By convention, the name of a record type starts with a "T". A record type declaration has the following syntax:

typerecordTypeName=recordlistField1:type1;...listFieldn:typen;end

For example, the following declaration creates a record type calledTDateRec.

typeTDateRec=recordYear:Integer;Month:(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);Day:1..31;end;

Each TDateRec contains three fields: an integer value calledYear, an enumerate value calledMonth and another integer value between 1 and 31 calledDay. The identifiers Year, Month and Day are names of fields of TDateRec that behave as variables. However, the TDateRec type declaration doesn't allocate memory for the Year, Month and Day fields. The memory is allocated when you instantiate a record, which you do as follows:

varRecord1,Record2:TDateRec;

The variable declaration creates two instances of TDateRec, called Record1 and Record2.

You can access to a record field by qualify the name of the field with the name of the record:

Record1.Year:=1922;Record1.Month:=Nov;Record1.Day:=26;

Or by using thewith instruction:

withRecord1dobeginYear:=1922;Month:=Nov;Day:=26;end;

Examples

[edit |edit source]

The following example asks a question, retrieves the answer and prints the answer:

var_Input,_Linkage:string;beginWriteLn('Hello, what is your name?');ReadLn(_Input);_Linkage:='Hello, '+_Input;WriteLn(_Linkage);end.

ReadLn(_Input) means that the application waits for the user to enter a text and then saves the text in the variable_Input.

This other example gets the number of a char in the ASCII:

varnumber:Integer;// Declare number as an integer_Input:string;_Char1:char;beginWriteLn('Enter a char!');ReadLn(_Input);_Char1:=_Input[1];number:=Ord(_Char1);WriteLn('ASCII is: ',number);end.

With _Input[1] you get the first char of _Input. And the first char is char 1, (not char 0 like inC++!)

Retrieved from "https://en.wikibooks.org/w/index.php?title=Delphi_Programming/Data_types&oldid=4234941"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp