473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Impossible? Who called the Method?

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
Jan 13 '06 #1
10 1255
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

"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

Jan 13 '06 #2
Thanks a lot!!
I'm using 2005, so I will take a look at it!

"guy" <gu*@discussions.microsoft.com> wrote in message
news:97**********************************@microsof t.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

Jan 13 '06 #3
guy
sorry should be TraceEventCache.CallStack
or you could use Environment.StackTrace

"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

Jan 13 '06 #4
Pieter,

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

Cor
Jan 13 '06 #5
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]" <no************@planet.nl> wrote in message
news:OB*************@TK2MSFTNGP11.phx.gbl...
Pieter,

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

Cor

Jan 13 '06 #6
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]" <no************@planet.nl> escribió en el mensaje
news:OB*************@TK2MSFTNGP11.phx.gbl...
Pieter,

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

Cor

Jan 13 '06 #7
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

"Pieter" <pi**********@hotmail.com> schreef in bericht
news:OJ*************@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]" <no************@planet.nl> wrote in message
news:OB*************@TK2MSFTNGP11.phx.gbl...
Pieter,

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

Cor


Jan 13 '06 #8
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]" <ca*****@NOSPAMsogecable.com> wrote in message
news:Oj****************@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]" <no************@planet.nl> escribió en el mensaje
news:OB*************@TK2MSFTNGP11.phx.gbl...
Pieter,

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

Cor


Jan 13 '06 #9
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.MethodImplAttribut e 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
"Pieter" <pi**********@hotmail.com> wrote in message
news:OJ*************@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]" <no************@planet.nl> wrote in message
| news:OB*************@TK2MSFTNGP11.phx.gbl...
| > Pieter,
| >
| > Why do you not pass the sender if you want to know that as it is
normally
| > done?
| >
| > Cor
| >
|
|
Jan 13 '06 #10
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
"Pieter" <pi**********@hotmail.com> escribió en el mensaje
news:ep*************@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]" <ca*****@NOSPAMsogecable.com> wrote in
message news:Oj****************@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]" <no************@planet.nl> escribió en el mensaje
news:OB*************@TK2MSFTNGP11.phx.gbl...
Pieter,

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

Cor



Jan 16 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: huzz | last post by:
I need to write script in c# that will scan directories for files and insert the files and directory names in the database.. I've have two tables tblDir and tblDocs. Example: -Directory1...
3
by: Brian Birtle | last post by:
**** A CHALLENGE TO THE GURUS - refute the statement "It's impossible to build a file upload progress meter using ASP.NET" **** First person to prove me wrong gets "All Time .NET Programming GOD"...
10
by: Pieter | last post by:
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...
1
by: John A Grandy | last post by:
I am trying to track down a mysterious null reference exception that occurs on making a call to a method in a assembly referenced by my project ( i.e. flow-of-control never arrives at the code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.