Tuesday, October 3, 2017

ປະເພດຂໍ້ມຸນຕົວປ່ຽນໃນ Delphi (Lesson 2)

ໃນພາສາ Delphi ມີ ປະເພດຂໍ້ມູນຫລາກຫລາຍຄືກັບພາສາໂປຣແກຣມອື່ນໆ
ຕົວຢ່າງ ການປະກາດຕົວປ່ຽນ

var                      // This starts a section of variables
   LineTotal : Integer;   // This defines an Integer variable called LineTotal
   First,Second : String; // This defines two variables to hold strings of text

ຈາກໂຄດຂ້າງເທິງ ເຫັນວ່າ ການເລີ່ມຕົ້ນດ້ວຍຊື່ຕົວປ່ຽນ ແລ້ວຕາມດ້ວຍ ປະເພດຂໍ້ມູນຕົວປ່ຽນ ສຶ່ງສຳຄັນ ຊື່ຂອງຕົວປ່ຽນຕ້ອງແມ່ນບໍ່ຊ້ຳກັນ ແລະ ການປະກາດຕົວປ່ຽນ ຈະບໍ່ຈຳແນກ ຕົວອັກສອນ ໃຫຍ່ ຫລື ນ້ອຍ

ຂໍ້ມູນປະເພດຕົວເລກ

Var
   // Integer data types :
   Int1 : Byte;     //                        0 to 255
   Int2 : ShortInt; //                     -127 to 127
   Int3 : Word;     //                        0 to 65,535
   Int4 : SmallInt; //                  -32,768 to 32,767
   Int5 : LongWord; //                        0 to 4,294,967,295
   Int6 : Cardinal; //                        0 to 4,294,967,295
   Int7 : LongInt;  //           -2,147,483,648 to 2,147,483,647
   Int8 : Integer;  //           -2,147,483,648 to 2,147,483,647
   Int9 : Int64;  // -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
 
   // Decimal data types :
   Dec1 : Single;   //  7  significant digits, exponent   -38 to +38
   Dec2 : Currency; // 50+ significant digits, fixed 4 decimal places
   Dec3 : Double;   // 15  significant digits, exponent  -308 to +308
   Dec4 : Extended; // 19  significant digits, exponent -4932 to +4932

ຂໍ້ມູນປະເພດຂໍ້ຄວາມອັກສອນ

 var
   Str1 : Char;        // Holds a single character, small alphabet
   Str2 : WideChar;    // Holds a single character, International alphabet
   Str3 : AnsiChar;    // Holds a single character, small alphabet
   Str4 : ShortString; // Holds a string of up to 255 Char's
   Str5 : String;      // Holds strings of Char's of any size desired
   Str6 : AnsiString;  // Holds strings of AnsiChar's any size desired
   Str7 : WideString;  // Holds strings of WideChar's of any size desired

ຂໍ້ມູນປະເພດ Logica

var
   Log1 : Boolean;     // Can be 'True' or 'False'

ປະກາດຕົວປ່ຽນ

ຂໍ້ມູນປະເພດ Sets, enumerations and subtypes

 type
   TSuit = (Hearts, Diamonds, Clubs, Spades);    // Defines the enumeration
 var
   suit : TSuit;  
type
   TWeek = Set of 1..7;             // Set comprising the days of the week, by number
 var
   week : TWeek;
 begin
   week := [1,2,3,4,5];      // Switch on the first 5 days of the week
 end;

ຕົວຢ່າງ ການກຳນົດຄ່າຈາກຕົວປ່ຽນ

types
   TWeek = 1..7;             // Set comprising the days of the week, by number
   TSuit = (Hearts, Diamonds, Clubs, Spades);    // Defines an enumeration
 
 const
   FRED          = 'Fred';       // String constant
   YOUNG_AGE     = 23;           // Integer constant
   TALL : Single = 196.9;        // Decimal constant
   NO            = False;        // Boolean constant
 
 var
   FirstName, SecondName : String;   // String variables
   Age                   : Byte;     // Integer variable
   Height                : Single;   // Decimal variable
   IsTall                : Boolean;  // Boolean variable
   OtherName             : String;   // String variable
   Week                  : TWeek;    // A set variable
   Suit                  : TSuit;    // An enumeration variable
 
 begin   // Begin starts a block of code statements
   FirstName  := FRED;          // Assign from predefined constant
   SecondName := 'Bloggs';      // Assign from a literal constant
   Age        := YOUNG_AGE;     // Assign from predefined constant
   Age        := 55;            // Assign from constant - overrides YOUNG_AGE
   Height     := TALL - 5.5;    // Assign from a mix of constants
   IsTall     := NO;            // Assign from predefined constant 
   OtherName  := FirstName;     // Assign from another variable
   Week       := [1,2,3,4,5];   // Switch on the first 5 days of the week
   Suit       := Diamonds;      // Assign to an enumerated variable
 end;    // End finishes a block of code statements
FirstName  is now set to 'Fred'
 SecondName is now set to 'Bloggs'
 Age        is now set to 55
 Height     is now set to 191.4
 IsTall     is now set to False
 OtherName  is now set to 'Fred'
 Week       is now set to 1,2,3,4,5
 Suit       is now set to Diamonds  (Notice no quotes)

ແບບ Arrays

var
   Suits : array[1..4] of String;    // A list of 4 playing card suit names
 
 begin
   Suits[1] := 'Hearts';    // Assigning to array index 1
   Suits[2] := 'Diamonds';  // Assigning to array index 2
   Suits[3] := 'Clubs';     // Assigning to array index 3
   Suits[4] := 'Spades';    // Assigning to array index 4
 end;

ແບບ Records

 type
   TCustomer Record
     firstName : string[20];
     lastName  : string[20];
     age       : byte;
   end;
 var
   customer : TCustomer;            // Our customer variable
 begin
   customer.firstName := 'Fred';    // Assigning to the customer record
   customer.lastName  := 'Bloggs';
   customer.age       := 55;
 end;
customer.firstName is now set to 'Fred'
 customer.lastName  is now set to 'Bloggs'
 customer.age       is now set to 55

ປະເພດຂໍ້ມູນອື່ນໆ
ເປັນ object types:

Files
File variables represent computer disk files. You can read from and write to these files using file access routines. This is a complex topic covered in Files.

Pointers
Pointers are also the subject of an advanced topic - see Pointer reference. They allow variables to be indirectly referenced.

Variants
Variants are also an advanced topic - see Variant. They allow the normal Delphi rigid type handling to be avoided. Use with care!

Subscribe

  • RSS Atom

ອອນລາຍ: 1 | ມື້ນີ້: 13 | ວານນີ້: 25 | ທິດນີ້: 93 | ເດືອນນີ້: 872 | ປີນີ້: 11832 | ລວມ: 78935