public class LogIT
{
public string strMessage = string.Empty;
public string strClassName = string.Empty;
public string strMethodName = string.Empty;
public LogIT()
{
strMethodName = new StackFrame(1).GetMethod().Name;
strClassName = new StackFrame(1).GetMethod().DeclaringType.Name;
}
}
Another class which inherits the LogIT class named "product"
My question is when i call the product class's GetProduct method i want logit class constructor to be called and known which method of dervied class has been called.
Please suggest how to accomplish it or any other alternative for the same.
Thanks
the easiest way to answer such questions for yourself is to create a simple experiment in your favourite tool for experimenting with c# ... for example, you could create a
console application in Visual Studio.
insert Console.WriteLine statements in the appropriate places in your code and then run your code.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
well in simple cases you cannot achieve what you are looking for because the base class constructor will get called firstly when you create instance of your class. This happens before you call any of your methods. If you want to set the values of your base
class properties or variables, do it directly from your method body like below:
public void GetProduct()
{
base.strMethodName="GetProduct";
}
Ashutosh Pathak
Blog: http://catchcode.blogspot.com Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
Hungarian notation is the practice of including a prefix in identifiers to encode some metadata about the parameter, such as the data type of the identifier.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
i used my favourite tool for experimenting with c#.
here's the code, similar to your code.
void Main()
{
Product product = new Product();
product.GetProduct();
}
public class LogIT
{
public String message = String.Empty;
public String className = String.Empty;
public String methodName = String.Empty;
public LogIT()
{
Console.WriteLine("Logit constructor was called");
methodName = new StackFrame(1).GetMethod().Name;
className = new StackFrame(1).GetMethod().DeclaringType.Name;
Console.WriteLine("Logit constructor is exiting");
}
}
public class Product : LogIT
{
public Product()
{
Console.WriteLine("Product constructor was called");
}
public void GetProduct()
{
Console.WriteLine("GetProduct method was called");
}
public void AddEditProduct()
{
Console.WriteLine("AddEditProduct method was called");
}
}
here's the output:
Logit constructor was called
Logit constructor is exiting
Product constructor was called
GetProduct method was called
and here's the corresponding IL (Intermediate Language):
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Marked as answer by Mark - MSFT on Nov 13, 2012 07:17 AM
You cannot do exactly what you have asked for quite fundamental reasons. The logIT constructor will be called when you create a Product instance (new Product()). The GetProduct method will be called sometime later and the constructor is not called again.
We need to know what happens next, eg what are you trying to achieve?
napster0613
Member
52 Points
12 Posts
How to know derived class which method is called/executed from the base class
Nov 07, 2012 07:07 AM|LINK
I have a class called logIT
public class LogIT
{
public string strMessage = string.Empty;
public string strClassName = string.Empty;
public string strMethodName = string.Empty;
public LogIT()
{
strMethodName = new StackFrame(1).GetMethod().Name;
strClassName = new StackFrame(1).GetMethod().DeclaringType.Name;
}
}
Another class which inherits the LogIT class named "product"
public class Product : LogIT
{
public Product()
{
}
public void GetProduct()
{
}
public void AddEditProduct()
{
}
}
THis is my main method:
static void Main(string[] args)
{
Product objProduct = new Product();
objProduct.GetProduct();
}
My question is when i call the product class's GetProduct method i want logit class constructor to be called and known which method of dervied class has been called.
Please suggest how to accomplish it or any other alternative for the same.
Thanks
gerrylowry
All-Star
20513 Points
5712 Posts
Re: How to know derived class which method is called/executed from the base class
Nov 07, 2012 07:56 AM|LINK
@ napster0613
the easiest way to answer such questions for yourself is to create a simple experiment in your favourite tool for experimenting with c# ... for example, you could create a console application in Visual Studio.
insert Console.WriteLine statements in the appropriate places in your code and then run your code.
g.
Ashutosh Pat...
Contributor
5737 Points
1105 Posts
Re: How to know derived class which method is called/executed from the base class
Nov 07, 2012 07:57 AM|LINK
well in simple cases you cannot achieve what you are looking for because the base class constructor will get called firstly when you create instance of your class. This happens before you call any of your methods. If you want to set the values of your base class properties or variables, do it directly from your method body like below:
public void GetProduct() { base.strMethodName="GetProduct"; }Blog: http://catchcode.blogspot.com
Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
gerrylowry
All-Star
20513 Points
5712 Posts
Re: How to know derived class which method is called/executed from the base class
Nov 07, 2012 08:06 AM|LINK
@ napster0613 TIMTOWTDI =. there is more than one way to do it
style note: prefixing your variables is not necessary given the strength of intellisense.
instead of "objProduct", simple write "product":
g.
http://msdn.microsoft.com/en-us/library/vstudio/ms229045(v=vs.100).aspx
"General Naming Conventions"
Do not use Hungarian notation.
Hungarian notation is the practice of including a prefix in identifiers to encode some metadata about the parameter, such as the data type of the identifier.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: How to know derived class which method is called/executed from the base class
Nov 07, 2012 08:21 AM|LINK
@ napster0613
i used my favourite tool for experimenting with c#.
here's the code, similar to your code.
void Main() { Product product = new Product(); product.GetProduct(); }public class LogIT { public String message = String.Empty; public String className = String.Empty; public String methodName = String.Empty; public LogIT() { Console.WriteLine("Logit constructor was called"); methodName = new StackFrame(1).GetMethod().Name; className = new StackFrame(1).GetMethod().DeclaringType.Name; Console.WriteLine("Logit constructor is exiting"); } }public class Product : LogIT { public Product() { Console.WriteLine("Product constructor was called"); } public void GetProduct() { Console.WriteLine("GetProduct method was called"); } public void AddEditProduct() { Console.WriteLine("AddEditProduct method was called"); } }here's the output:
and here's the corresponding IL (Intermediate Language):
g.
Paul Linton
Star
13403 Points
2531 Posts
Re: How to know derived class which method is called/executed from the base class
Nov 08, 2012 07:57 PM|LINK
You cannot do exactly what you have asked for quite fundamental reasons. The logIT constructor will be called when you create a Product instance (new Product()). The GetProduct method will be called sometime later and the constructor is not called again.
We need to know what happens next, eg what are you trying to achieve?