2015-10-07 6 views
0

Я хотел бы передать объект Scripting.Dictionary для Application.OnTime. Проблема в том, что я продолжаю получать «Ошибка компиляции: аргумент не факультативен».Передача словаря в Application.OnTime

Структура кода заключается в следующем:

Public Sub mainSub() 

    Dim orderBookIndex As New Scripting.Dictionary 
    Dim contractIndex As New Scripting.Dictionary 

    Call computeStuff(0, orderBookIndex, contractIndex) 

End Sub 

mainSub называет computeStuff, то computeStuff называет «callLive», чтобы запустить себя каждый второй после того, как увеличивается.

Public Sub computeStuff(index As Integer, Optional orderBookIndex As Scripting.Dictionary, Optional contractIndex As Scripting.Dictionary) 

    ' Do stuff 

    If index = 0 Then 
     Set orderBookIndex = New Scripting.Dictionary 
     Set contractIndex = New Scripting.Dictionary 
    End If 

    If index = 1 Then 

     Set orderBookIndex = New Scripting.Dictionary 
     Set contractIndex = New Scripting.Dictionary 

     orderBookIndex.add "a", "b" ' add random stuff 

     contractIndex.add "c", "d" ' add random stuff 

    End If 

    Call callLive(index, orderBookIndex, contractIndex) 

End Sub 

Тогда callLive называется и отказывается компилировать:

Public Sub callLive(index As Integer, Optional orderBookIndex As Scripting.Dictionary, Optional contractIndex As Scripting.Dictionary) 

    Dim SchTime As Double 

    SchTime = Now + TimeSerial(0, 0, 1) 
    index = index + 1 
    Application.OnTime SchTime, "'computeStuff """ & index & """ ,""" & orderBookIndex & """ ,""" & contractIndex & """'", , False 

End Sub 

Любая помощь или руководство для достижения этой задачи (или решить эту проблему) будут высоко оценены.

+0

Обычно рекомендуется держать глобальные переменные в минимальном использовании, но, почему бы не попробовать: сделать переменные словаря orderbookIndex 'и 'contractIndex'? –

ответ

0

Try, имеющий orderBookIndex и contractIndex, как глобальные переменные, а не как ниже и посмотреть, если это работает для вас:

Public orderBookIndex As Scripting.Dictionary 
Public contractIndex As Scripting.Dictionary 

Public Sub mainSub() 

Set orderBookIndex = New Scripting.Dictionary 
Set contractIndex = New Scripting.Dictionary 

Call computeStuff(0, orderBookIndex, contractIndex) 

End Sub 
Public Sub computeStuff(index As Integer, Optional orderBookIndex As Scripting.Dictionary, Optional contractIndex As Scripting.Dictionary) 

' Do stuff 

If index = 0 Then 
    Set orderBookIndex = New Scripting.Dictionary 
    Set contractIndex = New Scripting.Dictionary 
End If 

If index = 1 Then 

    Set orderBookIndex = New Scripting.Dictionary 
    Set contractIndex = New Scripting.Dictionary 

    orderBookIndex.Add "a", "b" ' add random stuff 

    contractIndex.Add "c", "d" ' add random stuff 

End If 

Call callLive(index, orderBookIndex, contractIndex) 

End Sub 

Public Sub callLive(index As Integer, orderBookIndex As Scripting.Dictionary, contractIndex As Scripting.Dictionary) 

Dim SchTime As Double 

SchTime = Now + TimeSerial(0, 0, 1) 
index = index + 1 
Application.OnTime SchTime, "'computeStuff " & index & " , orderBookIndex, contractIndex'" 

End Sub 
Смежные вопросы