472,342 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 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 1480
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...
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...
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...
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...
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...
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...
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...
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...
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.