Impossible? Who called the Method?

P

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
 
G

Guest

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
 
C

Cor Ligthert [MVP]

Pieter,

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

Cor
 
P

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?
 
C

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
 
C

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
 
P

Pieter

I don't understand what you are saying :-S
Can you explain it a little bit more practical? With(in) an example?
 
J

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


| 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?
|
| | > Pieter,
| >
| > Why do you not pass the sender if you want to know that as it is
normally
| > done?
| >
| > Cor
| >
|
|
 
C

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
 

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