วัตถุพจนานุกรม ASP
ออบเจ็กต์พจนานุกรมเก็บข้อมูลเป็นคู่ชื่อ/ค่า
ตัวอย่างเพิ่มเติม
วิธีสร้างวัตถุพจนานุกรม จากนั้นใช้วิธีที่มีอยู่เพื่อตรวจสอบว่ามีคีย์ที่ระบุอยู่หรือไม่
วิธีการใช้เมธอด Items เพื่อส่งคืนอาร์เรย์ของรายการทั้งหมด
วิธีใช้วิธีคีย์เพื่อส่งคืนอาร์เรย์ของคีย์ทั้งหมด
วิธีการใช้คุณสมบัติ Item เพื่อคืนค่าของสินค้า
วิธีใช้คุณสมบัติคีย์เพื่อตั้งค่าคีย์ในออบเจกต์พจนานุกรม
วิธีใช้คุณสมบัติ Count เพื่อส่งคืนจำนวนคู่ของคีย์/รายการ
วัตถุพจนานุกรม
ออบเจ็กต์ Dictionary ใช้เพื่อเก็บข้อมูลในคู่ชื่อ/ค่า (เรียกว่าคีย์และรายการ) ออบเจ็กต์ Dictionary อาจดูคล้ายกับ Arrays อย่างไรก็ตาม อ็อบเจ็กต์ Dictionary เป็นโซลูชันที่ต้องการมากกว่าในการจัดการข้อมูลที่เกี่ยวข้อง
การเปรียบเทียบพจนานุกรมและอาร์เรย์:
- คีย์ใช้เพื่อระบุรายการในวัตถุพจนานุกรม
- คุณไม่จำเป็นต้องเรียก ReDim เพื่อเปลี่ยนขนาดของวัตถุพจนานุกรม
- เมื่อลบรายการออกจากพจนานุกรม รายการที่เหลือจะเลื่อนขึ้นโดยอัตโนมัติ
- พจนานุกรมไม่สามารถมีหลายมิติได้ Array can
- พจนานุกรมมีฟังก์ชันในตัวมากกว่า Arrays
- พจนานุกรมทำงานได้ดีกว่าอาร์เรย์ในการเข้าถึงองค์ประกอบสุ่มบ่อยๆ
- พจนานุกรมทำงานได้ดีกว่าอาร์เรย์ในการค้นหารายการตามเนื้อหา
ตัวอย่างต่อไปนี้สร้างอ็อบเจ็กต์ Dictionary เพิ่มคู่คีย์/ไอเท็ม และดึงค่าไอเท็มสำหรับคีย์ gr:
<%
Dim d
Set d=Server.CreateObject("Scripting.Dictionary")
d.Add "re","Red"
d.Add "gr","Green"
d.Add "bl","Blue"
d.Add "pi","Pink"
Response.Write("The value of key gr is: " & d.Item("gr"))
%>
Output:
The value of key gr is: Green
คุณสมบัติและวิธีการของอ็อบเจ็กต์ Dictionary อธิบายไว้ด้านล่าง:
คุณสมบัติ
Property | Description |
---|---|
CompareMode | Sets or returns the comparison mode for comparing keys in a Dictionary object |
Count | Returns the number of key/item pairs in a Dictionary object |
Item | Sets or returns the value of an item in a Dictionary object |
Key | Sets a new key value for an existing key value in a Dictionary object |
วิธีการ
Method | Description |
---|---|
Add | Adds a new key/item pair to a Dictionary object |
Exists | Returns a Boolean value that indicates whether a specified key exists in the Dictionary object |
Items | Returns an array of all the items in a Dictionary object |
Keys | Returns an array of all the keys in a Dictionary object |
Remove | Removes one specified key/item pair from the Dictionary object |
RemoveAll | Removes all the key/item pairs in the Dictionary object |