2010-07-06 14 views
5

In VBScript, alcuni oggetti incorporati utilizzano una proprietà senza nome. Alcuni esempi:Proprietà predefinita senza nome

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") 

Come posso utilizzare la stessa sintassi nelle mie classi?

UPDATE

Ecco un lavoro, ad esempio stand-alone di come fare questo, un semplice wrapper per Scripting.Dictionary. Notare l'uso di "Let" per consentire la sintassi d ("chiave") = "valore". Naturalmente il merito va a Thom per aver fornito la risposta.

<% 
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") 
%> 

risposta

2

È necessario dichiarare una proprietà della classe come proprietà predefinita. A titolo di esempio, ecco parte di una classe String involucro ho scritto:

class StringClass 
    private finished_ 
    private data_ 
    private size_ 

    public function init (val) 
     finished_ = cStr(val) 
     set init = me 
    end function 

    public default property get value 
     if (size_ > 0) then 
      finished_ = finished_ & join(data_, vbNullString) 
      data_ = empty 
      size_ = 0 
     end if 
     value = finished_ 
    end property 

    public property let value (val) 
     data_ = empty 
     size_ = empty 
     init(val) 
    end property 

    public function add (s) 
     size_ = size_ + 1 
     if (isEmpty(data_)) then 
      redim data_(MIN_ARRAY_SIZE) 
     elseif (size_ > uBound(data_)) then 
      redim preserve data_(Float(uBound(data_) * GRANTED_HEAD_ROOM).ceil) 
     end if 
     data_(size_ - 1) = cStr(s) 
    end function 
end class 

utilizzo: dim s: set s = new StringClass s() = "Ciao, mondo!" 's.value() = "Ciao, mondo!" Response.Write 's s.value Response.Write()

si può anche avere una proprietà predefinita parametrizzata:

class ListClass 
    private size_ 
    private data_ 

    private sub CLASS_INITIALIZE 
     size_ = 0 
     data_ = Array() 
     resize_array MIN_ARRAY_SIZE 
    end sub 

    public default property get data (index) 
     if isObject(data) then 
      set data_(index) = data 
     else 
      data_(index) = data 
     end if 
    end property 

    public property let data (index, value) 
     data_(index) = value 
    end property 

    public property set data (index, value) 
     set data_(index) = value 
    end property 

    public function add(datum) 
     size_ = size_ + 1 
     if (size_ > uBound(data_) + 1) then expand_array 

     assign data_(size_ - 1), datum 

     add = datum 
    end function 
end class 

dim l: set l = new ListClass 
l.add("Hello, world!") 
l(0) = "Goodbye, world!" 
Response.Write l(0) 

Questo secondo esempio è probabilmente quello che cercate, utilizzando le proprietà di default per implementare collezioni, ma vale la pena di verificare il primo esempio, utilizzando le proprietà predefinite per implementare l'auto-unboxing delle classi wrapper.

Problemi correlati