Dictionary ใน AL Programming เป็นชนิดข้อมูลที่ใช้สำหรับเก็บและจัดการข้อมูลที่มีความสัมพันธ์กันระหว่างตัวระบุ (Key) และข้อมูลที่เกี่ยวข้อง (Value) โดยที่ตัวระบุ (Key) จะต้องไม่ซ้ำกันภายใน Dictionary ซึ่งช่วยให้สามารถค้นหาและเข้าถึงข้อมูลได้อย่างรวดเร็วและมีประสิทธิภาพ การใช้งาน Dictionary เหมาะสำหรับสถานการณ์ที่ต้องการจัดการข้อมูลจำนวนมากที่สามารถอ้างอิงได้โดยใช้ตัวระบุเฉพาะ ทำให้การเพิ่ม, ลบ, แก้ไข, และดึงข้อมูลเป็นไปได้อย่างสะดวกและยืดหยุ่น Dictionary เป็นเครื่องมือที่ทรงพลังใน AL Programming ที่ช่วยให้นักพัฒนาสามารถสร้างระบบที่มีการจัดการข้อมูลอย่างมีประสิทธิภาพและมีความยืดหยุ่นสูง
รายละเอียดของแต่ละ Method พร้อมตัวอย่าง และการใช้งาน
1. Add(TKey, TValue)
คำอธิบาย: Method นี้ใช้ในการเพิ่มข้อมูลเข้าไปใน Dictionary โดยข้อมูลนี้จะถูกเก็บในรูปแบบที่มีตัวระบุ (Key) และค่าที่เกี่ยวข้อง (Value) หากตัวระบุตัวนี้มีอยู่แล้วใน Dictionary จะเกิดข้อผิดพลาด
ตัวอย่าง
procedure AddToDictionary(var Dict: Dictionary of [Integer, Text]; Id: Integer; Value: Text)
begin
if not Dict.ContainsKey(Id) then begin
Dict.Add(Id, Value);
Message('Added Key %1 with Value %2', Id, Value);
end else
Error('Key %1 already exists.', Id);
end;
การใช้งาน
var
MyDictionary: Dictionary of [Integer, Text];
begin
AddToDictionary(MyDictionary, 1, 'Apple');
AddToDictionary(MyDictionary, 2, 'Banana');
end;
ในตัวอย่างนี้ AddToDictionary จะเพิ่มข้อมูลใหม่ 1: 'Apple' และ 2: 'Banana' ลงใน Dictionary
2. ContainsKey(TKey)
คำอธิบาย: Method นี้ใช้ในการตรวจสอบว่าตัวระบุ (Key) ที่กำหนดมีอยู่ใน Dictionary หรือไม่ ถ้ามีจะคืนค่าเป็น true ถ้าไม่มีจะคืนค่าเป็น false
ตัวอย่าง
procedure CheckKeyExists(var Dict: Dictionary of [Integer, Text]; Id: Integer): Boolean
begin
if Dict.ContainsKey(Id) then
Message('Key %1 exists in the dictionary', Id)
else
Message('Key %1 does not exist in the dictionary', Id);
exit(Dict.ContainsKey(Id));
end;
การใช้งาน
var
MyDictionary: Dictionary of [Integer, Text];
begin
MyDictionary.Add(1, 'Apple');
CheckKeyExists(MyDictionary, 1);
CheckKeyExists(MyDictionary, 3);
end;
ในตัวอย่างนี้ CheckKeyExists จะตรวจสอบว่ามีตัวระบุ 1 และ 3 อยู่ใน Dictionary หรือไม่
3. Count()
คำอธิบาย: Method นี้ใช้ในการนับจำนวนรายการข้อมูลทั้งหมดใน Dictionary
ตัวอย่าง
procedure GetDictionaryCount(var Dict: Dictionary of [Integer, Text]): Integer
begin
exit(Dict.Count());
end;
การใช้งาน
var
MyDictionary: Dictionary of [Integer, Text];
ItemCount: Integer;
begin
MyDictionary.Add(1, 'Apple');
MyDictionary.Add(2, 'Banana');
ItemCount := GetDictionaryCount(MyDictionary);
Message('Total items in dictionary: %1', ItemCount);
end;
ในตัวอย่างนี้ GetDictionaryCount จะคืนค่าจำนวนรายการข้อมูลใน Dictionary
4. Get(TKey, var TValue)
คำอธิบาย: Method นี้ใช้ในการดึงข้อมูลออกจาก Dictionary โดยใช้ตัวระบุ (Key) ที่กำหนด หากตัวระบุตัวนี้มีอยู่ ข้อมูลที่เกี่ยวข้อง (Value) จะถูกดึงออกมา และคืนค่าเป็น true มิฉะนั้นจะคืนค่าเป็น false
ตัวอย่าง
procedure GetValueFromDictionary(var Dict: Dictionary of [Integer, Text]; Id: Integer; var Value: Text): Boolean
begin
if Dict.Get(Id, Value) then begin
Message('The value for Key %1 is %2', Id, Value);
exit(true);
end else begin
Message('Key %1 not found in the dictionary', Id);
exit(false);
end;
end;
การใช้งาน
var
MyDictionary: Dictionary of [Integer, Text];
Value: Text;
begin
MyDictionary.Add(1, 'Apple');
GetValueFromDictionary(MyDictionary, 1, Value);
end;
ในตัวอย่างนี้ GetValueFromDictionary จะดึงค่า Apple ออกมาเมื่อระบุคีย์ 1
5. Get(TKey)
คำอธิบาย: Method นี้คล้ายกับ Get(TKey, var TValue) แต่จะคืนค่าที่ตรงกับตัวระบุ (Key) ที่กำหนดโดยตรง โดยไม่ต้องใช้ตัวแปรส่งออก
ตัวอย่าง
procedure GetValueDirectFromDictionary(var Dict: Dictionary of [Integer, Text]; Id: Integer): Text
var
Value: Text;
begin
Value := Dict.Get(Id);
Message('The value for Key %1 is %2', Id, Value);
exit(Value);
end;
การใช้งาน
var
MyDictionary: Dictionary of [Integer, Text];
begin
MyDictionary.Add(1, 'Apple');
GetValueDirectFromDictionary(MyDictionary, 1);
end;
ในตัวอย่างนี้ GetValueDirectFromDictionary จะดึงค่า Apple ออกมาเมื่อระบุคีย์ 1 และคืนค่ากลับมาโดยตรง
6. Keys()
คำอธิบาย: Method นี้ใช้ในการดึงรายการตัวระบุ (Key) ทั้งหมดที่มีอยู่ใน Dictionary
ตัวอย่าง
procedure GetAllKeys(var Dict: Dictionary of [Integer, Text]): List of [Integer]
var
KeyCollection: List of [Integer];
Key: Integer;
begin
KeyCollection := Dict.Keys();
foreach Key in KeyCollection do
Message('Key: %1', Key);
exit(KeyCollection);
end;
การใช้งาน
var
MyDictionary: Dictionary of [Integer, Text];
begin
MyDictionary.Add(1, 'Apple');
MyDictionary.Add(2, 'Banana');
GetAllKeys(MyDictionary);
end;
ในตัวอย่างนี้ GetAllKeys จะดึงรายการตัวระบุทั้งหมดใน Dictionary และแสดงผลคีย์แต่ละตัว
7. Remove(TKey)
คำอธิบาย: Method นี้ใช้ในการลบข้อมูลที่ตรงกับตัวระบุ (Key) ที่กำหนดออกจาก Dictionary
ตัวอย่าง
procedure RemoveFromDictionary(var Dict: Dictionary of [Integer, Text]; Id: Integer)
begin
if Dict.ContainsKey(Id) then begin
Dict.Remove(Id);
Message('Key %1 has been removed', Id);
end else
Message('Key %1 not found in the dictionary', Id);
end;
การใช้งาน
var
MyDictionary: Dictionary of [Integer, Text];
begin
MyDictionary.Add(1, 'Apple');
RemoveFromDictionary(MyDictionary, 1);
end;
ในตัวอย่างนี้ RemoveFromDictionary จะลบข้อมูล 1: 'Apple' ออกจาก Dictionary
8. Set(TKey, TValue)
คำอธิบาย: Method นี้ใช้ในการเปลี่ยนหรือเพิ่มข้อมูลใน Dictionary โดยใช้ตัวระบุ (Key) ที่กำหนด หากตัวระบุ (Key) มีอยู่แล้ว ข้อมูลที่เกี่ยวข้อง (Value) จะถูกแทนที่ด้วยค่าที่ใหม่
ตัวอย่าง
procedure SetDictionaryValue(var Dict: Dictionary of [Integer, Text]; Id: Integer; Value: Text)
begin
Dict.Set(Id, Value);
Message('Key %1 is now associated with %2', Id, Value);
end;
การใช้งาน
var
MyDictionary: Dictionary of [Integer, Text];
begin
MyDictionary.Add(1, 'Apple');
SetDictionaryValue(MyDictionary, 1, 'Orange');
end;
ในตัวอย่างนี้ SetDictionaryValue จะเปลี่ยนค่าที่คีย์ 1 จาก Apple เป็น Orange
9. Set(TKey, TValue, var TValue)
คำอธิบาย: Method นี้ใช้ในการเปลี่ยนหรือเพิ่มข้อมูลใน Dictionary โดยใช้ตัวระบุ (Key) ที่กำหนด และยังคืนค่าข้อมูลเก่าที่ถูกแทนที่กลับมา
ตัวอย่าง
procedure SetDictionaryValueWithOld(var Dict: Dictionary of [Integer, Text]; Id: Integer; Value: Text): Text
var
OldValue: Text;
begin
Dict.Set(Id, Value, OldValue);
Message('Key %1 was associated with %2, now with %3', Id, OldValue, Value);
exit(OldValue);
end;
การใช้งาน
var
MyDictionary: Dictionary of [Integer, Text];
OldValue: Text;
begin
MyDictionary.Add(1, 'Apple');
OldValue := SetDictionaryValueWithOld(MyDictionary, 1, 'Orange');
end;
ในตัวอย่างนี้ SetDictionaryValueWithOld จะเปลี่ยนค่าที่คีย์ 1 จาก Apple เป็น Orange และคืนค่าเดิม Apple กลับมา
10. Values()
คำอธิบาย: Method นี้ใช้ในการดึงค่าข้อมูลทั้งหมดที่มีอยู่ใน Dictionary
ตัวอย่าง
procedure GetAllValues(var Dict: Dictionary of [Integer, Text]): List of [Text]
var
ValueCollection: List of [Text];
Value: Text;
begin
ValueCollection := Dict.Values();
foreach Value in ValueCollection do
Message('Value: %1', Value);
exit(ValueCollection);
end;
การใช้งาน
var
MyDictionary: Dictionary of [Integer, Text];
begin
MyDictionary.Add(1, 'Apple');
MyDictionary.Add(2, 'Banana');
GetAllValues(MyDictionary);
end;
ในตัวอย่างนี้ GetAllValues จะดึงค่าข้อมูลทั้งหมดใน Dictionary และแสดงผลค่าแต่ละตัว
ตัวอย่างเพิ่มเติม
procedure SummarizeOutstandingAmountByVendor()
var
VendorLedgerEntry: Record "Vendor Ledger Entry";
VendorOutstandingDict: Dictionary of [Code[20], Decimal];
VendorNo: Code[20];
OutstandingAmount: Decimal;
begin
VendorLedgerEntry.SetAutoCalcFields(Amount);
VendorLedgerEntry.SetRange("Open", true); // กำหนดช่วงให้เลือกเฉพาะรายการที่ยังไม่ปิด
if VendorLedgerEntry.FindSet() then
repeat
VendorNo := VendorLedgerEntry."Vendor No.";
if VendorOutstandingDict.ContainsKey(VendorNo) then begin
// อัปเดตยอดค้างชำระหากมีผู้ขายคนเดียวกันใน Dictionary
VendorOutstandingDict.Get(VendorNo, OutstandingAmount);
OutstandingAmount := OutstandingAmount + VendorLedgerEntry.Amount;
VendorOutstandingDict.Set(VendorNo, OutstandingAmount);
end else begin
// เพิ่มผู้ขายใหม่และยอดค้างชำระเข้าไปใน Dictionary
VendorOutstandingDict.Add(VendorNo, VendorLedgerEntry.Amount);
end;
until VendorLedgerEntry.Next() = 0;
// อ่านและแสดงผลแต่ละ Vendor No. และ Outstanding Amount
foreach VendorNo in VendorOutstandingDict.Keys() do begin
VendorOutstandingDict.Get(VendorNo, OutstandingAmount);
Message('Vendor No: %1, Outstanding Amount: %2', VendorNo, OutstandingAmount);
end;
end;
คำอธิบาย
- VendorLedgerEntry.SetRange(“Open”, true);: กำหนดให้เลือกเฉพาะรายการที่ยังไม่ถูกปิด (Open Entries)
- VendorOutstandingDict.ContainsKey(VendorNo): ตรวจสอบว่ามี Vendor No. อยู่ใน Dictionary แล้วหรือไม่
- VendorOutstandingDict.Set(VendorNo, OutstandingAmount);: อัปเดตยอดค้างชำระรวมสำหรับผู้ขายที่มีอยู่ใน Dictionary
- VendorOutstandingDict.Add(VendorNo, VendorLedgerEntry.Amount);: เพิ่มผู้ขายใหม่พร้อมยอดค้างชำระเข้าไปใน Dictionary
สรุป
การใช้งาน Dictionary ใน AL Programming ช่วยให้คุณสามารถจัดการข้อมูลที่ต้องการเข้าถึงอย่างรวดเร็วและมีประสิทธิภาพ การใช้ Method ต่าง ๆ ที่มีใน Dictionary ช่วยให้สามารถเพิ่ม, ลบ, อัปเดต, และดึงข้อมูลได้อย่างสะดวกและมีประสิทธิภาพ ซึ่งทำให้การพัฒนาแอปพลิเคชันที่มีการจัดการข้อมูลจำนวนมากเป็นไปได้อย่างมีประสิทธิภาพและยืดหยุ่น
