473,396 Members | 1,810 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.

Which class is calling?

I have a base class with a method that is to be called in the constructor of
the inheritting classes. Is there any way of determining, say, the Type of
the class that is calling it.

e.g.

class A
{
public A()
{
AddStuff();
}

protected void AddStuff()
{
//do stuff
}
}

class B : A
{
public B()
{
AddStuff();
}
}

class C : B
{
public C()
{
AddStuff();
}
}

So when I create an instance of C, AddStuff() will be called by each of the
three constructors, but I need to know which constructor was calling it (so
in know the context to which I should add the stuff).

Hope that makes some sort of sense.

Greg
Nov 15 '05 #1
8 1560
Hi Greg,

You would have to examine the call stack which is expensive and unreliable
IMO.
You might consider another way to solve your problem.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Greg Bacchus" <FB**********@spammotel.com> wrote in message
news:uU**************@TK2MSFTNGP10.phx.gbl...
I have a base class with a method that is to be called in the constructor of the inheritting classes. Is there any way of determining, say, the Type of
the class that is calling it.

e.g.

class A
{
public A()
{
AddStuff();
}

protected void AddStuff()
{
//do stuff
}
}

class B : A
{
public B()
{
AddStuff();
}
}

class C : B
{
public C()
{
AddStuff();
}
}

So when I create an instance of C, AddStuff() will be called by each of the three constructors, but I need to know which constructor was calling it (so in know the context to which I should add the stuff).

Hope that makes some sort of sense.

Greg

Nov 15 '05 #2
You really need to change your design. Why can't each constructor do its own
init as appropriate? if all classes have a block of init code in common,
this can go in a protected method in the base class.

Just in case someone suggests it, calling virtual methods from a constructor
is a really bad idea, since the virtual method calls happen before any other
initialisation that the constructor does. This can produce unexpected
results.

Hope this helps

Regards

Ron

"Greg Bacchus" <FB**********@spammotel.com> wrote in message
news:uU**************@TK2MSFTNGP10.phx.gbl...
I have a base class with a method that is to be called in the constructor of the inheritting classes. Is there any way of determining, say, the Type of
the class that is calling it.

e.g.

class A
{
public A()
{
AddStuff();
}

protected void AddStuff()
{
//do stuff
}
}

class B : A
{
public B()
{
AddStuff();
}
}

class C : B
{
public C()
{
AddStuff();
}
}

So when I create an instance of C, AddStuff() will be called by each of the three constructors, but I need to know which constructor was calling it (so in know the context to which I should add the stuff).

Hope that makes some sort of sense.

Greg

Nov 15 '05 #3
Miha,

I am curious, can you show examples of where the call stack is
unreliable? I'll definitely agree that it is expensive, but I haven't heard
claims that it is unreliable (and security would be compromised as a result,
because stack walks are done in order to handle a lot of the security in
..NET).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Miha Markic" <miha at rthand com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Greg,

You would have to examine the call stack which is expensive and unreliable
IMO.
You might consider another way to solve your problem.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Greg Bacchus" <FB**********@spammotel.com> wrote in message
news:uU**************@TK2MSFTNGP10.phx.gbl...
I have a base class with a method that is to be called in the constructor
of
the inheritting classes. Is there any way of determining, say, the Type

of the class that is calling it.

e.g.

class A
{
public A()
{
AddStuff();
}

protected void AddStuff()
{
//do stuff
}
}

class B : A
{
public B()
{
AddStuff();
}
}

class C : B
{
public C()
{
AddStuff();
}
}

So when I create an instance of C, AddStuff() will be called by each of

the
three constructors, but I need to know which constructor was calling it

(so
in know the context to which I should add the stuff).

Hope that makes some sort of sense.

Greg


Nov 15 '05 #4
Hi Nicholas,

I am not sure on reliablity, AFAIK compiler could optimize some calls, do
things inline - stuff like that.
Even if it is not doing this already it can in future.
I didn't ever test it though.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP11.phx.gbl...
Miha,

I am curious, can you show examples of where the call stack is
unreliable? I'll definitely agree that it is expensive, but I haven't heard claims that it is unreliable (and security would be compromised as a result, because stack walks are done in order to handle a lot of the security in
.NET).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Miha Markic" <miha at rthand com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Greg,

You would have to examine the call stack which is expensive and unreliable
IMO.
You might consider another way to solve your problem.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Greg Bacchus" <FB**********@spammotel.com> wrote in message
news:uU**************@TK2MSFTNGP10.phx.gbl...
I have a base class with a method that is to be called in the constructor
of
the inheritting classes. Is there any way of determining, say, the
Type of the class that is calling it.

e.g.

class A
{
public A()
{
AddStuff();
}

protected void AddStuff()
{
//do stuff
}
}

class B : A
{
public B()
{
AddStuff();
}
}

class C : B
{
public C()
{
AddStuff();
}
}

So when I create an instance of C, AddStuff() will be called by each

of the
three constructors, but I need to know which constructor was calling
it (so
in know the context to which I should add the stuff).

Hope that makes some sort of sense.

Greg



Nov 15 '05 #5
<"Miha Markic" <miha at rthand com>> wrote:
I am not sure on reliablity, AFAIK compiler could optimize some calls, do
things inline - stuff like that.
Even if it is not doing this already it can in future.
I didn't ever test it though.


The JIT inlining functions can definitely have a nasty effect on a
stack trace. (I was suspicious of this until I actually saw it.) Here's
a program which demonstrates it:

using System;
using System.Diagnostics;

class Test
{
static StackTrace GetTrace()
{
return new System.Diagnostics.StackTrace();
}

static StackTrace Intermediate()
{
return GetTrace();
}

static void Main(string[] args)
{
Console.WriteLine (Intermediate());
}
}

Compiled with debug:
at Test.GetTrace()
at Test.Intermediate()
at Test.Main(String[])

Compiled without debug information:
at Test.Main(String[])

(Adding [MethodImpl(MethodImplOptions.NoInlining)] to Intermediate and
GetTrace makes it get it right.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
greg,

it does sound like your design could be organized in a better way. i'm not
exactly sure what behavior you're going for, but it seems like you want
this:

when C is constructed, the C object calls the B constructor, which calls
the A constructor, which adds stuff for A, then the B constructor adds
stuff for B, then the C constructor adds stuff for C.

if AddStuff works in different ways depending on which class it is called
from, eg it does action 1 when A calls it, action 2 when B calls it, and
action 3 when C calls it, then AddStuff should really be a different method
in each of these classes. i suggest looking into the virtual and new
keywords for this. this is a common problem in object oriented development
which is solved by Polymorphism. i've posted one example below - notice i
defined A's AddStuff with the virtual keyword and the others are defined
with the new keyword in the method signature. when you run this code, the
output is A->B->C.

let me know if you have any other questions.

jeff.

namespace ConsoleApplication4
{
class Class1
{
static void Main(string[] args)
{
C c = new C();
Console.ReadLine();
}
}

class A
{
public A()
{
this.AddStuff();
}

public virtual void AddStuff()
{
Console.WriteLine("AddStuff - A");
}
}
class B : A
{
public B()
{
this.AddStuff();
}

public new void AddStuff()
{
Console.WriteLine("AddStuff - B");
}
}
class C : B {
public C()
{
this.AddStuff();
}

public new void AddStuff()
{
Console.WriteLine("AddStuff - C");
}
}
}

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

Nov 15 '05 #7
*goes crosseyed*
"Jeff Callahan" <Je*****@online.microsoft.com> wrote in message
news:f6**************@cpmsftngxa07.phx.gbl...
greg,

it does sound like your design could be organized in a better way. i'm not exactly sure what behavior you're going for, but it seems like you want
this:

when C is constructed, the C object calls the B constructor, which calls
the A constructor, which adds stuff for A, then the B constructor adds
stuff for B, then the C constructor adds stuff for C.

if AddStuff works in different ways depending on which class it is called
from, eg it does action 1 when A calls it, action 2 when B calls it, and
action 3 when C calls it, then AddStuff should really be a different method in each of these classes. i suggest looking into the virtual and new
keywords for this. this is a common problem in object oriented development which is solved by Polymorphism. i've posted one example below - notice i
defined A's AddStuff with the virtual keyword and the others are defined
with the new keyword in the method signature. when you run this code, the
output is A->B->C.

let me know if you have any other questions.

jeff.

namespace ConsoleApplication4
{
class Class1
{
static void Main(string[] args)
{
C c = new C();
Console.ReadLine();
}
}

class A
{
public A()
{
this.AddStuff();
}

public virtual void AddStuff()
{
Console.WriteLine("AddStuff - A");
}
}
class B : A
{
public B()
{
this.AddStuff();
}

public new void AddStuff()
{
Console.WriteLine("AddStuff - B");
}
}
class C : B {
public C()
{
this.AddStuff();
}

public new void AddStuff()
{
Console.WriteLine("AddStuff - C");
}
}
}

--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

Nov 15 '05 #8
Hi Greg

I posted this last night, but it didn't turn up on the NG - not an unknown
event methinks.

You really need to change your design. Why can't each constructor do its own
init as appropriate? if all classes have a block of init code in common,
this can go in a protected method in the base class.

Just in case someone suggests it, calling virtual methods from a constructor
is a really bad idea, since the virtual method calls happen before any other
initialisation that the constructor does. This can produce unexpected
results.

Hope this helps

Regards

Ron
Nov 15 '05 #9

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

Similar topics

4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
5
by: Buddy Ackerman | last post by:
I have taken over an application that has a sealed (singleton) class for database access. I want to add a private SQLConnection class variable and open the connection it whenever it is instantiated...
10
by: jim.brown | last post by:
Please refer me to the right place if this is the wrong place to post this question. I'm looking for an example of calling the Eigenvalue routines of JAMA from a C++ program. The documentation...
7
by: Gustavo L. Fabro | last post by:
Greetings! Some classes that once compiled without problems on VS 2003 have now problems on VS 2005 Beta 1. I'm talking about a __nogc class that is exported with __declspec(dllexport). The...
3
by: Jim Heavey | last post by:
I create a class which would hold my static methods (methods I want to call without having to instantiate the class). I then attempted to use it in another class and I get the following error...
8
by: Jeffrey Barish | last post by:
I believe that the answer to my question is no, but I want to be sure that I understand this issue correctly: Suppose that there are two classes defined as follows: class A(object): def...
5
by: ffrugone | last post by:
My scenario involves two classes and a database. I have the classes "Broom" and "Closet". I want to use a static method from the "Closet" class to search the database for a matching "Broom". If...
2
by: Steven D'Aprano | last post by:
Here's a simple class-factory function that returns a sub-class of the old-style class it is passed. def verbosify_oclass(klass): """Returns a verbose sub-class of old-style klass.""" class...
7
by: Peter Bradley | last post by:
OK. A bit behind the times, I know; but we're just moving over to .NET 2.0. How on earth do you manage configuration settings in a class library in .NET 2.0? In version 1.1, we used a handy class...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.