User Tools

Site Tools


tutorial:variableanddatatype

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tutorial:variableanddatatype [2019/01/07 15:39]
admin [Standard Type]
tutorial:variableanddatatype [2019/01/11 14:14] (current)
admin [Enumerated Type]
Line 19: Line 19:
 </​sxh>​ </​sxh>​
  
-     +\\ \\
 =====Data Types===== =====Data Types=====
 แบ่งได้เป็น 4 ประเภท ดังนี้ แบ่งได้เป็น 4 ประเภท ดังนี้
Line 29: Line 28:
 | Structured Type | Array \\ Record \\ Object \\ Class | | Structured Type | Array \\ Record \\ Object \\ Class |
 | Pointer Type | Pointer | | Pointer Type | Pointer |
 +\\ 
 +\\
 =====Standard Type===== =====Standard Type=====
 สรุปได้ดังนี้ สรุปได้ดังนี้
Line 37: Line 37:
   x,​y:​integer;​ </​sxh>​ |<sxh delphi;> ​   x,​y:​integer;​ </​sxh>​ |<sxh delphi;> ​
 x:=10; x:=10;
-y:=$23; //Hex+y:=$10; //Hex = 16
 </​sxh>​ | </​sxh>​ |
 |Real|<​sxh delphi;> |Real|<​sxh delphi;>
Line 64: Line 64:
 </​sxh>​ | </​sxh>​ |
  
 +\\ \\
 +=====User-Define Type=====
 +=====Enumerated Type=====
 +คือ Data Type ที่เรากำหนดชื่อได้เอง ซึ่งชื่อที่กำหนดขึ้นต้องเป็นไปตามกฎการตั้งชื่อตัวแปร ไม่ใช่ชื่อในแบบ string โดยสิ่งที่สำคัญสำหรับ Data Type ประเภทนี้ คือ การมีลำดับตัวเลขเป็นของตัวเอง ทำให้ Enumerated Type ถูกนำมาเปรียบเทียบกันด้วยเครื่องหมาย =,>,< ได้เหมือนๆกับตัวเลขทั่วไป
 +
 +การประกาศ Enumerated Type ทำได้ดังนี้
 +
 +<sxh delphi;>
 +Type
 +  TDay = (Mon,​Tue,​Wed,​Thu,​Fri,​Sat,​Sun);​
 +  TDirection = (North = 2, East, South = 8, West);
 +</​sxh> ​
 + 
 +การเรียกใช้งาน
 +
 +<sxh delphi;>
 +VAR
 +  ToDay:TDay;
 +  Direction:​TDirection;​
 +BEGIN
 +  Today:=Wed;
 +  Direction:​=EAst;​ //case insensitive
 +  ​
 +  if  Today < Sat then
 +    writeln('​Today is not holliday'​)
 +  else
 +    writeln('​Today is holliday'​);​
 +    ​
 +  writeln('​Your direction number is : ',​ord(Direction)); ​   ​
 +  readln;
 +END.
 +</​sxh> ​
 +
 +เราสามารถหาเลขลำดับของ Enumerated Variable ได้ โดยใช้ฟังก์ชั่น Ord() จากตัวอย่างข้างบน หากไม่มีการกำหนดลำดับให้ ลำดับก็จะเริ่มจาก 0 เสมอ จะได้ลำดับของแต่ละวันใน TDay ดังนี้
 +
 +  Mon=0, Tue=1, Wed=2, Thu=3, Fri=4, Sat=5, Sun=6 
 +
 +สำหรับ Enumerated Variable ที่มีการกำหนดลำดับ ก็จะได้ลำดับตามที่กำหนดมา แต่สำหรบตัวที่ไม่ได้กำหนด จะนำลำดับตัวก่อนหน้ามา +1 จากตัวอย่าง TDirection จะได้ลำดับดังนี้
 +
 +  North = 2, East = 3, South = 8, West = 9
 +
 +จากตัวอย่างข้างบน บรรทัดที่ 6 จะสังเกตว่า การใส่ชื่อเป็น Case Insensitive เหมือนกันกับการตั้งชื่อตัวแปรในภาษา Pascal ดังนั้น East, EAST, EAst, eAsT เหล่านี้ ย่อมเป็นตัวเดียวกัน
tutorial/variableanddatatype.1546850367.txt.gz · Last modified: 2019/01/07 15:39 by admin