andreas said:
I want to do some calculation like
( t1 and t2 are known)
for i = t1 to t2
for j = t1 to t2
.....
.....
for p = t1 to t2
for q = t1 to t2
some actions
next
next
.......
next
next
but i know only the number for next in run time with the variable iTimes
<snip>
One possible approach would be to use a single function controlled by
iTimes: if iTimes was 0, it would perform the specified action,
otherwise it would call itself recursevelly, decrementing iTimes. The
matters complicate a little because you want the respective indexes of
each loop, but these can be kept in a stack and accessed by the
'action' sub.
An air-code example of this approach could be:
Class NestedLoop
Protected ReadOnly Stack As New Stack(Of Integer)
Protected ReadOnly Min As Integer
Protected ReadOnly Max As Integer
Protected Levels As Integer
Sub New(Levels As Integer, Min As Integer, Max As Integer)
Me.Min = Min
Me.Max = Max
Me.Levels = Levels
End Sub
Protected Sub Execute
DoLoop(Levels)
End Sub
Protected Overridable Sub Action()
End Sub
Private Sub DoLoop(Level As Integer)
If Level = 0 Then
Action
Else
For I As Integer = Min To Max
Stack.Push(I)
DoLoop(Level - 1)
I = Stack.Pop
Next
End If
End Sub
End Class
Then, it would be just a matter of inheriting from the class and
overriding the method Action, adding to the derived class whatever new
properties you'd like.
Class SimpleLoop
Inherits NestedLoop
Public ReadOnly Result As Integer
Public Sub New(Levels As Integer, Min As Integer, Max As Integer)
MyBase.New(Levels, Min, Max)
Execute
End Sub
Protected Overrides Sub Action
Result += 1
End Sub
End Class
Finally, to invoke the action, you could use something like this:
Dim ActionResult As New SimpleLoop(iTimes, T1, T2)
Presto! =)
HTH.
Regards,
Branco.