0

is there any way to know the sender of a simple function ?

 Public Function functionA() As Integer
 functionB()
 End Function

 Public Function functionB() As Integer
 ' i need to do another processing if it comes from functionA

 End Function

any ideas ?

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
user1187282
  • 1,137
  • 4
  • 14
  • 23
  • 1
    You could get a snapshot using a StackTrace and see the calling method. http://www.csharp-examples.net/reflection-calling-method-name/ – Tomas McGuinness Feb 12 '13 at 12:08

3 Answers3

4

The solution for your problem is wrong. You do not need to know the calling function name, you need to refactor your code (what if you want to rename the method later, or call it from another method?).

Add for example a boolean parameter which is false by default, but set it to true if you are calling the function from FunctionA().

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
1

As CodeCaster said, you should approach your problem in a different way, heres a example code:

Public Function functionA() As Integer
    Call functionB(True)
End Function

Public Function functionB(Optional bFromA As Boolean = False) As Integer
    If bFromA Then
       ...
    Else
       ...
    End If

End Function

And you could even create your own set of options in order to call your function from different places with different processing options:

Public Enum OptionTypeB
    DefaultOpt
    OptionName1
    OptionName2
    OptionName3
End Enum

Public Function functionB(Optional bOptions As OptionTypeB = OptionTypeB.DefaultOpt) As Integer
    ....
End Function
SysDragon
  • 9,692
  • 15
  • 60
  • 89
0
Console.WriteLine(new StackFrame(1, true).GetMethod().Name);

Above code should give you the calling method name.

Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42