Ciao,
una Sub può restituire un valore:
(fonte: B4X Basic Language)
Ma se io, all'interno di questa sub ci metto un Wait For una Resumable Sub mi restituisce il seguente errore "Resumable subs return type must be ResumableSub (or none).", ad esempio:
(questo è solo un esempio basato a sua volta sull'esempio presente nel booklet. Ho inserito nell'esempio HttpJob perché è quello per cui faccio fatica a trovare un workaround).
Pare che l'alternativa sia rendere anche la Sub CalcInterestWithSpread ResumableSub, ossia
Questo, però, implica che non posso assegnare ad una variabile direttamente la sub
ma devo utilizzare un Wait for anche in questa sub:
Questo è quello che ho capito.
C'è qualcosa che mi sfugge?*
Grazie!
* Se mi sta sfuggendo qualcosa, dei dettagli su cosa mi sfugge sarebbero graditi.
una Sub può restituire un valore:
4.5.6 Returned value
A sub can return a value, this can be any object.
Returning a value is done with the Return keyword.
The type of the return value is added after the parameter list.
B4X:
Sub CalcInterest(Capital As Double, Rate As Double) As Double
Return Capital * Rate / 100
End Sub
Ma se io, all'interno di questa sub ci metto un Wait For una Resumable Sub mi restituisce il seguente errore "Resumable subs return type must be ResumableSub (or none).", ad esempio:
B4X:
Sub CalcInterestWithSpread(Capital As Double, Rate As Double) As Double
Dim myJob As HttpJob
myJob.Initialize("", Me)
...
myJob.Download($"${URLWhereICanDownloadTheSpread}"$)
Wait For (myJob) JobDone(myJob As HttpJob)
...
Return Capital * Rate * Spread/ 100
End Sub
Pare che l'alternativa sia rendere anche la Sub CalcInterestWithSpread ResumableSub, ossia
B4X:
Sub CalcInterestWithSpread(Capital As Double, Rate As Double) As ResumableSub
Private Spread As String
Private myJob As HttpJob
myJob.Initialize("", Me)
...
myJob.Download($"${URLWhereICanDownloadTheSpread}"$)
Wait For (myJob) JobDone(myJob As HttpJob)
Spread = myJob.GetString
...
Private interest as Double = Capital * Rate * Spread/ 100
Return interest
End Sub
Questo, però, implica che non posso assegnare ad una variabile direttamente la sub
B4X:
Private Sub Interest ' dà errore
Private interest as Double
interest = CalcInterestWithSpread(1000.88, 0.4)
Log(interest)
End Sub
ma devo utilizzare un Wait for anche in questa sub:
B4X:
Private Sub Interest
Private interest as Double
Wait For (CalcInterestWithSpread(1000.88, 0.4)) Complete (Result as Double)
interest = Result
Log(interest) ' Potrei inserire direttamente Result ma nella maggior parte dei casi ci sono altre operazioni
End Sub
Questo è quello che ho capito.
C'è qualcosa che mi sfugge?*
Grazie!
* Se mi sta sfuggendo qualcosa, dei dettagli su cosa mi sfugge sarebbero graditi.
Last edited: