Trong VBScript, một số đối tượng được dựng sẵn sử dụng thuộc tính chưa đặt tên. Một số ví dụ:Tài sản mặc định không được đặt tên
Set Dict = Server.CreateObject("Scripting.Dictionary")
Set RS = GetEmloyeesRecordSet()
Dict("Beer") = "Tasty" ' Same as Dict.Item("Beer") = "Tasty"
Dict("Crude Oil") = "Gross" ' Same as Dict.Item("Crude Oil") = "Gross"
Response.Write "The First Employee Is: " & RS("Name") ' Same as RS.Fields("Name")
Tôi có thể sử dụng cùng cú pháp này trong lớp học của riêng mình như thế nào?
CẬP NHẬT
Đây là một hoạt động, độc lập ví dụ về làm thế nào để làm điều này, một wrapper đơn giản cho Scripting.Dictionary. Lưu ý việc sử dụng "Let" để cho phép cú pháp d ("key") = "value". Tất nhiên tín dụng đi đến Thom để cung cấp câu trả lời.
<%
Class DictWrapper
Private Dict
Private Sub Class_Initialize()
Set Dict = Server.CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
Set Dict = Nothing
End Sub
Public Property Get Count
Count = Dict.Count
End Property
Public Default Property Get Item(Key)
Item = Dict(Key)
End Property
Public Property Let Item(Key, Value)
Dict(Key) = Value
End Property
Public Sub Add(Key, Value)
Dict.Add Key, Value
End Sub
End Class
Dim d : Set d = New DictWrapper
d.Add "Beer", "Good"
Response.Write d("Beer") & "<br>"
d("Beer") = "Bad"
Response.Write d("Beer")
%>