Home All Groups Group Topic Archive Search About

Impossible? Who called the Method?

Author
13 Jan 2006 9:17 AM
Pieter
Hi,

How can I know inside a given method, by which method it was called?

For instance: I have two property's (Prenom and Nom), both call the Method
PropertyChangedHandler(). How Can I know if PropertyChangedHandle was called
by the Prenom or by Nom?

    Public Event PrenomChanged As EventHandler
    Public Property Prenom() As String
        Get
            Return m_strPrenom
        End Get
        Set(ByVal Value As String)
            m_strPrenom = Value
            RaiseEvent PrenomChanged(Me, New EventArgs())
            PropertyChangedHandler()
        End Set
    End Property

    Public Event NomChanged As EventHandler
    Public Property Nom() As String
        Get
            Return m_strNom
        End Get
        Set(ByVal Value As String)
            m_strNom = Value
            RaiseEvent NomChanged(Me, New EventArgs())
            PropertyChangedHandler()
        End Set
    End Property

     Private Sub PropertyChangedHandler()
          'I Want to know in this Method which property called it: Prenom or
Nom...
     End Sub



I could put the name of the property in a paramter like this:
PropertyChangedHandler("Nom") etc. But in my opinion it's not really a nice
solution :-) So is there any way to do it with a solution without everytime
having to use a unique variable? something like the sender-object?

Thanks a lot in advance,

Pieter

Author
13 Jan 2006 9:40 AM
guy
TraceEvent.Callstack might be of use if you are using vb2005

if you want to pass the method name as a parameter use
MethodBase.GetCurrentMethod.Name
so that if you change the method name the new name gets passed

hth

guy

Show quoteHide quote
"Pieter" wrote:

> Hi,
>
> How can I know inside a given method, by which method it was called?
>
> For instance: I have two property's (Prenom and Nom), both call the Method
> PropertyChangedHandler(). How Can I know if PropertyChangedHandle was called
> by the Prenom or by Nom?
>
>     Public Event PrenomChanged As EventHandler
>     Public Property Prenom() As String
>         Get
>             Return m_strPrenom
>         End Get
>         Set(ByVal Value As String)
>             m_strPrenom = Value
>             RaiseEvent PrenomChanged(Me, New EventArgs())
>             PropertyChangedHandler()
>         End Set
>     End Property
>
>     Public Event NomChanged As EventHandler
>     Public Property Nom() As String
>         Get
>             Return m_strNom
>         End Get
>         Set(ByVal Value As String)
>             m_strNom = Value
>             RaiseEvent NomChanged(Me, New EventArgs())
>             PropertyChangedHandler()
>         End Set
>     End Property
>
>      Private Sub PropertyChangedHandler()
>           'I Want to know in this Method which property called it: Prenom or
> Nom...
>      End Sub
>
>
>
> I could put the name of the property in a paramter like this:
> PropertyChangedHandler("Nom") etc. But in my opinion it's not really a nice
> solution :-) So is there any way to do it with a solution without everytime
> having to use a unique variable? something like the sender-object?
>
> Thanks a lot in advance,
>
> Pieter
>
>
>
Author
13 Jan 2006 9:51 AM
Pieter
Thanks a lot!!
I'm using 2005, so I will take a look at it!

Show quoteHide quote
"guy" <g**@discussions.microsoft.com> wrote in message
news:97BCC1C1-0FC5-49B4-AAF3-F2800D29A156@microsoft.com...
> TraceEvent.Callstack might be of use if you are using vb2005
>
> if you want to pass the method name as a parameter use
> MethodBase.GetCurrentMethod.Name
> so that if you change the method name the new name gets passed
>
> hth
>
> guy
>
> "Pieter" wrote:
>
>> Hi,
>>
>> How can I know inside a given method, by which method it was called?
>>
>> For instance: I have two property's (Prenom and Nom), both call the
>> Method
>> PropertyChangedHandler(). How Can I know if PropertyChangedHandle was
>> called
>> by the Prenom or by Nom?
>>
>>     Public Event PrenomChanged As EventHandler
>>     Public Property Prenom() As String
>>         Get
>>             Return m_strPrenom
>>         End Get
>>         Set(ByVal Value As String)
>>             m_strPrenom = Value
>>             RaiseEvent PrenomChanged(Me, New EventArgs())
>>             PropertyChangedHandler()
>>         End Set
>>     End Property
>>
>>     Public Event NomChanged As EventHandler
>>     Public Property Nom() As String
>>         Get
>>             Return m_strNom
>>         End Get
>>         Set(ByVal Value As String)
>>             m_strNom = Value
>>             RaiseEvent NomChanged(Me, New EventArgs())
>>             PropertyChangedHandler()
>>         End Set
>>     End Property
>>
>>      Private Sub PropertyChangedHandler()
>>           'I Want to know in this Method which property called it: Prenom
>> or
>> Nom...
>>      End Sub
>>
>>
>>
>> I could put the name of the property in a paramter like this:
>> PropertyChangedHandler("Nom") etc. But in my opinion it's not really a
>> nice
>> solution :-) So is there any way to do it with a solution without
>> everytime
>> having to use a unique variable? something like the sender-object?
>>
>> Thanks a lot in advance,
>>
>> Pieter
>>
>>
>>
Author
13 Jan 2006 9:52 AM
guy
sorry should be TraceEventCache.CallStack
or you could use Environment.StackTrace

Show quoteHide quote
"guy" wrote:

> TraceEvent.Callstack might be of use if you are using vb2005
>
> if you want to pass the method name as a parameter use
> MethodBase.GetCurrentMethod.Name
> so that if you change the method name the new name gets passed
>
> hth
>
> guy
>
> "Pieter" wrote:
>
> > Hi,
> >
> > How can I know inside a given method, by which method it was called?
> >
> > For instance: I have two property's (Prenom and Nom), both call the Method
> > PropertyChangedHandler(). How Can I know if PropertyChangedHandle was called
> > by the Prenom or by Nom?
> >
> >     Public Event PrenomChanged As EventHandler
> >     Public Property Prenom() As String
> >         Get
> >             Return m_strPrenom
> >         End Get
> >         Set(ByVal Value As String)
> >             m_strPrenom = Value
> >             RaiseEvent PrenomChanged(Me, New EventArgs())
> >             PropertyChangedHandler()
> >         End Set
> >     End Property
> >
> >     Public Event NomChanged As EventHandler
> >     Public Property Nom() As String
> >         Get
> >             Return m_strNom
> >         End Get
> >         Set(ByVal Value As String)
> >             m_strNom = Value
> >             RaiseEvent NomChanged(Me, New EventArgs())
> >             PropertyChangedHandler()
> >         End Set
> >     End Property
> >
> >      Private Sub PropertyChangedHandler()
> >           'I Want to know in this Method which property called it: Prenom or
> > Nom...
> >      End Sub
> >
> >
> >
> > I could put the name of the property in a paramter like this:
> > PropertyChangedHandler("Nom") etc. But in my opinion it's not really a nice
> > solution :-) So is there any way to do it with a solution without everytime
> > having to use a unique variable? something like the sender-object?
> >
> > Thanks a lot in advance,
> >
> > Pieter
> >
> >
> >
Author
13 Jan 2006 10:48 AM
Cor Ligthert [MVP]
Pieter,

Why do you not pass the sender if you want to know that as it is normally
done?

Cor
Author
13 Jan 2006 11:20 AM
Pieter
How do you mean?
The nicest solution I have now is:
call the method as PropertyChangedHandler(MethodBase.GetCurrentMethod)

and than use that paramter in my PropertyChangedHandler-method. I can call
it "sender" there, but that doesn't matter really.

Is it that what you mean?

Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:OBnnP7CGGHA.344@TK2MSFTNGP11.phx.gbl...
> Pieter,
>
> Why do you not pass the sender if you want to know that as it is normally
> done?
>
> Cor
>
Author
13 Jan 2006 12:32 PM
Cor Ligthert [MVP]
Pieter,

Don't you think that this is more descriptive as you using

\\\\
Public Property Nom() As String
        Get
            Return m_strNom
        End Get
        Set(ByVal Value As String)
            m_strNom = Value
            RaiseEvent NomChanged(Me, New EventArgs())
            PropertyChangedHandler("Nom")
        End Set
End Property

Private Sub PropertyChangedHandler(ByVal prop As String)
        Select Case prop
            Case "Nom"
End Select
///

Than whatever late binding method. What you show is for me a good inbuild
program obfuscating method, so if you want to use it, what should I say.

I had to think by this about what I have read last week, that in that week a
Belgian had connected the water pipes in his house to the gas pipes and
everybody in that area was without water and gas.

However maybe I am wrong and does your solution work terrific.

:-)

Cor



Show quoteHide quote
"Pieter" <pietercou***@hotmail.com> schreef in bericht
news:OJ8XYNDGGHA.740@TK2MSFTNGP12.phx.gbl...
> How do you mean?
> The nicest solution I have now is:
> call the method as PropertyChangedHandler(MethodBase.GetCurrentMethod)
>
> and than use that paramter in my PropertyChangedHandler-method. I can call
> it "sender" there, but that doesn't matter really.
>
> Is it that what you mean?
>
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
> news:OBnnP7CGGHA.344@TK2MSFTNGP11.phx.gbl...
>> Pieter,
>>
>> Why do you not pass the sender if you want to know that as it is normally
>> done?
>>
>> Cor
>>
>
>
Author
13 Jan 2006 4:46 PM
Jay B. Harlow [MVP - Outlook]
Pieter,
| The nicest solution I have now is:
| call the method as PropertyChangedHandler(MethodBase.GetCurrentMethod)

May not provide consistent results. The JIT compiler is free to inline the
Prenom & Nom methods into the routine that called them, for example:


    Public Property Prenom() As String
        ...

    Public Property Nom() As String
        ...

    Public Sub DoSomething()
        Prenom = 1
        Nom = 2
    End SUb

The Prenom & Nom Set code may be inlined into the DoSomething routine, when
means MethodBase.GetCurrentMethod may return DoSomething & not Prenom or
Nom...

You can use System.Runtime.CompilerServices.MethodImplAttribute with the
MethodImplOptions.NoInlining to prevent the above problem. However using the
MethodImplOptions.NoInlining may prevent the JIT compiler from creating
optimal code. Be certain to profile the effects that the option may have.
Especially in release builds run outside the IDE.

Imports System.Runtime.CompilerServices

    <MethodImplAttribute(MethodImplOptions.NoInlining)> _
    Public Property Prenom() As String
        ...

FWIW:
Rather then use "New EventArgs":

            RaiseEvent PrenomChanged(Me, New EventArgs())

I would recommend using EventArgs.Empty, as it prevents a lot of temporary
objects, which limits GC pressure.

            RaiseEvent PrenomChanged(Me, EventArgs.Empty)

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Pieter" <pietercou***@hotmail.com> wrote in message
news:OJ8XYNDGGHA.740@TK2MSFTNGP12.phx.gbl...
| How do you mean?
| The nicest solution I have now is:
| call the method as PropertyChangedHandler(MethodBase.GetCurrentMethod)
|
| and than use that paramter in my PropertyChangedHandler-method. I can call
| it "sender" there, but that doesn't matter really.
|
| Is it that what you mean?
|
| "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
| news:OBnnP7CGGHA.344@TK2MSFTNGP11.phx.gbl...
| > Pieter,
| >
| > Why do you not pass the sender if you want to know that as it is
normally
| > done?
| >
| > Cor
| >
|
|
Author
13 Jan 2006 11:56 AM
Carlos J. Quintero [VB MVP]
Because there is no need for methods,  the .NET Framework provides the
infrastructure to retrieve that info dynamically...

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com


Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> escribió en el mensaje
news:OBnnP7CGGHA.344@TK2MSFTNGP11.phx.gbl...
> Pieter,
>
> Why do you not pass the sender if you want to know that as it is normally
> done?
>
> Cor
Author
13 Jan 2006 12:58 PM
Pieter
I don't understand what you are saying :-S
Can you explain it a little bit more practical? With(in) an example?

Show quoteHide quote
"Carlos J. Quintero [VB MVP]" <carlosq@NOSPAMsogecable.com> wrote in message
news:OjE%23yhDGGHA.1192@TK2MSFTNGP11.phx.gbl...
> Because there is no need for methods,  the .NET Framework provides the
> infrastructure to retrieve that info dynamically...
>
> --
>
> Best regards,
>
> Carlos J. Quintero
>
> MZ-Tools: Productivity add-ins for Visual Studio
> You can code, design and document much faster:
> http://www.mztools.com
>
>
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> escribió en el mensaje
> news:OBnnP7CGGHA.344@TK2MSFTNGP11.phx.gbl...
>> Pieter,
>>
>> Why do you not pass the sender if you want to know that as it is normally
>> done?
>>
>> Cor
>
>
Author
16 Jan 2006 1:17 PM
Carlos J. Quintero [VB MVP]
I meant that there is no need to hardcode in a string the name of the method
which is making a call since the called method can retrieve that information
using the .NET Framework classes. For example:

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
      Call f()
   End Sub

   Sub f()
      Dim s As New System.Diagnostics.StackTrace
      MessageBox.Show(s.GetFrame(1).GetMethod.ToString)
   End Sub

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com


Show quoteHide quote
"Pieter" <pietercou***@hotmail.com> escribió en el mensaje
news:epzfHEEGGHA.644@TK2MSFTNGP09.phx.gbl...
>I don't understand what you are saying :-S
> Can you explain it a little bit more practical? With(in) an example?
>
> "Carlos J. Quintero [VB MVP]" <carlosq@NOSPAMsogecable.com> wrote in
> message news:OjE%23yhDGGHA.1192@TK2MSFTNGP11.phx.gbl...
>> Because there is no need for methods,  the .NET Framework provides the
>> infrastructure to retrieve that info dynamically...
>>
>> --
>>
>> Best regards,
>>
>> Carlos J. Quintero
>>
>> MZ-Tools: Productivity add-ins for Visual Studio
>> You can code, design and document much faster:
>> http://www.mztools.com
>>
>>
>> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> escribió en el mensaje
>> news:OBnnP7CGGHA.344@TK2MSFTNGP11.phx.gbl...
>>> Pieter,
>>>
>>> Why do you not pass the sender if you want to know that as it is
>>> normally done?
>>>
>>> Cor
>>
>>
>
>