|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Impossible? Who called the Method?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 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 > > > 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 >> >> >> 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 > > > > > > Pieter,
Why do you not pass the sender if you want to know that as it is normally done? Cor 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 > 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. :-) CorShow 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 >> > > Pieter,
| The nicest solution I have now is: May not provide consistent results. The JIT compiler is free to inline the | call the method as PropertyChangedHandler(MethodBase.GetCurrentMethod) 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) -- Show quoteHide quoteHope this helps Jay [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "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 | > | | Because there is no need for methods, the .NET Framework provides the
infrastructure to retrieve that info dynamically... -- Show quoteHide quoteBest 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 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 > > 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 -- Show quoteHide quoteBest 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" <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 >> >> > >
string calculation
Why so much trouble with ellipses? Seeking examples of screen scraping.... Setting MaxLength to <VBFixedString(x)>? Grid setup - dataset -multiple tables Another Dataset question Pasting from the clipboard How Can I prevent a new row in datagrid But permit to edit existing cell ? Retrieving a webpage source HTM and checking for a string thereinL? User Control and Namespace question |
|||||||||||||||||||||||