2010-05-07 5 views
11

Sono una persona non VB6 che ha avuto la sfortuna di ereditare un progetto VB6/Classic ASP legacy buggato. C'è una sezione in cui molte voci sono inserite in un Dictionary e voglio vedere tutto ciò che contiene. Ho provato questo (oParams è un dizionario):Iterate attraverso un dizionario VB6

Dim o As Object 
Dim sDicTempAggr As String 
sDicTempAggr = "" 
For Each o In oParams 
    sDicTempAggr = sDicTempAggr & ", " & o 
Next 

che ha restituito:

oggetto non supporta questa proprietà o metodo: 438

Utilizzando Option Explicit, come faccio a iterare attraverso un VB6 Dictionary per scoprire tutto ciò che contiene?

risposta

12

Ecco un esempio per l'iterazione, se avete ancora uno sguardo problema al secondo ciclo di ispezionare i tipi di valori nel dizionario

Dim oParams As New Dictionary 
    oParams.Add 1, "This" 
    oParams.Add 2, "That" 
    oParams.Add 3, "The other" 
    Dim key As Variant 
    Dim sDicTempAggr As String 
    Dim sTypes As String 
    For Each key In oParams.Keys 
     sDicTempAggr = sDicTempAggr & IIf(sDicTempAggr <> "", ",", "") & oParams(key) 
    Next key 
    For Each key In oParams.Keys 
      sTypes = sTypes & IIf(sTypes <> "", ",", "") & TypeName(oParams(key)) 
    Next key 
9

Un'enumerazione nel corso di un dizionario non è di tipo Object, è necessario utilizzare Variant invece:

Dim o As Variant 
Dim sDicTempAggr As String 
sDicTempAggr = "" 

For Each o In oParams 
    sDicTempAggr = sDicTempAggr & ", " & oParams(o) 
Next 

Anche all'interno della For Each la chiave non è il membro dizionario restituito da qui il oParams(o) per il valore, se si passare a Per Each o In oParams.items è possibile utilizzare direttamente oParams.