FraserOfSmeg FraserOfSmeg - 2 months ago 12
Vb.net Question

How to call the sub that called this sub from a future sub

Boy oh boy, that title is confusing ay!

This is best explained with an example.. here's what I want to happen.

Dim m as sub

Public sub alg1()
call alg2()
End sub

Public sub alg2()
m = alg1
call alg3()
End sub

Public sub alg3()
call m
End sub


So I start running my code at alg1. alg1 calls alg2, which makes a note of who called it (I'm thinking with stackTrace.GetFrame(1).GetMethod().Name) and then stores that in a local variable and then alg2 goes on to call alg3. alg3 calls whichever sub is stored in the local variable m.

Now my situation is a little more complicated since alg1 is in a different class to alg2, but in theory all I'm trying to do is make a cross class call to a sub that's defined at runtime. Is there any way to store a sub like this?

Answer

Your example should work with a small change of Action and AddressOf

Dim m As Action

Public Sub alg1()
    Call alg2()
End Sub

Public Sub alg2()
    m = AddressOf alg1
    Call alg3()
End Sub

Public Sub alg3()
    Call m()
End Sub

Another option is to pass it as parameter:

Public Sub alg2()
    alg3(AddressOf alg1)
End Sub

Public Sub alg3(m As action)
    m()
End Sub