When that code is executed

M

Marc R.

Hello,

I have a function "DoSomething" that it is called from the constructor and
also from the code like in a click event. Is there a way to know, from my
DoSomething function, who has call this function? ex.:

Sub DoSomething
If Call = "New" then
'I am from Sub New
Else
'I am from some where else
End IF

End Sub

Hope that I am clear
 
H

Herfried K. Wagner [MVP]

Marc R. said:
I have a function "DoSomething" that it is called from the constructor and
also from the code like in a click event. Is there a way to know, from my
DoSomething function, who has call this function? ex.:

Sub DoSomething
If Call = "New" then
'I am from Sub New
Else
'I am from some where else
End IF

End Sub

No, there is no way to do that. However, you can add a parameter to your
procedure indicating from where it has been called.
 
P

Patrice

Though you could (throught reflection or by providing an explicit argument)
this would be really IMO a bad thing to do (what if you want then to call
from a third place or if you change the caller name or whatever).

My personal preference would be to split this having :
- a method that contains the common code
- a method that calls this common code + what is specific to the "new" case
- a method that calls this common code + what is specific to the "click"
case

Then just call the correct method from the constructor and the click event.

It models closely the situation you have that is you want to call common
code from both places but each place will call its own broadest work that in
turn calls common work to be done (instead of having each place calling
common code and deciding in this common code that you should finally do only
this or that because you have been called from a particular location)...
 
G

Guest

No, there is no way to do that. However, you can add a parameter to
your procedure indicating from where it has been called.

You can use reflection to find this information out correct?
 
P

Patrice

[cut]
You can use reflection to find this information out correct?


Yes but IMO it would be a wrong approach. The truth is that he just don't
want to call the same code so IMO the OP best bet would be to call two
distinct procedures (each one calling in turn the same procedure so that
they share same of the same code and then the code that is specific to each
case).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top