Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Determining the calling function name.

Posted on 2009-05-06
5
Medium Priority
?
348 Views
Last Modified: 2013-11-07
Simple situation:

public function foo() as boolean
    bar()
end function

public function foo2() as boolean
    bar()
end function

public sub bar()
   'code to determine whether the function was called from foo or foo2
end sub

Which code would I place in the sub bar() to determine the calling function, without having to send it to the sub manually?
0
Comment
Question by:Onthrax
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 9

Expert Comment

by:Deathrace
ID: 24313594
hey , i got a general idea,, is the below code helps you.

string whereimcalled = null; //declare globally
 
public function foo() as boolean
{
   whereimcalled = "Hey its method foo"; 
}
 
public function foo2() as boolean
{
  whereimcalled ="Hey its method foo2";
}
 
public subbar()
{
   // evaluate the string variable///
}
 
}

Open in new window

0
 
LVL 4

Expert Comment

by:cauos
ID: 24313614
you have to choices; you can pass extra parameter to the called function and then check the value of the passed parameter.
or you can use stacktrace class to know which is the calling function
  private void f1()
        {
            calledfunction();
        }
        private void f2()
        {
            calledfunction();
        }
        private void calledfunction()
        {
 
            System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1);
            System.Diagnostics.StackFrame sf = st.GetFrame(0);
            string msg =sf.GetMethod().Name;
            MessageBox.Show(msg);
 
        }

Open in new window

0
 
LVL 8

Expert Comment

by:OBonio
ID: 24313622
Have a look at the StackFrame and StackTrace classes.  I'm not sure what you're trying to achieve though.  If the function does something different based on who is calling it, then it should really have another function to run the different code....
0
 
LVL 4

Accepted Solution

by:
cauos earned 2000 total points
ID: 24313668
ok you are using vb not c#;
        sub f1()       
            calledfunction()
        end sub
        sub f2()
            calledfunction()
        sub 
        sub calledfunction()
 
            dim st as   new System.Diagnostics.StackTrace(1)
            dim sf as System.Diagnostics.StackFrame  = st.GetFrame(0)
            dim msg  as string =sf.GetMethod().Name
            MessageBox.Show(msg)
       end sub

Open in new window

0
 
LVL 13

Author Comment

by:Onthrax
ID: 24324334
Thanks all for replying.

The stacktrace solution will work just fine without any extra manual settings. Great!

Cheers :)

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question