473,662 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1584
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**********@s pammotel.com> wrote in message
news:uU******** ******@TK2MSFTN GP10.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**********@s pammotel.com> wrote in message
news:uU******** ******@TK2MSFTN GP10.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.co m

"Miha Markic" <miha at rthand com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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**********@s pammotel.com> wrote in message
news:uU******** ******@TK2MSFTN GP10.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.c om> wrote in
message news:%2******** ********@TK2MSF TNGP11.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.co m

"Miha Markic" <miha at rthand com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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**********@s pammotel.com> wrote in message
news:uU******** ******@TK2MSFTN GP10.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.Diagnost ics;

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

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

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

Compiled with debug:
at Test.GetTrace()
at Test.Intermedia te()
at Test.Main(Strin g[])

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

(Adding [MethodImpl(Meth odImplOptions.N oInlining)] to Intermediate and
GetTrace makes it get it right.)

--
Jon Skeet - <sk***@pobox.co m>
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 ConsoleApplicat ion4
{
class Class1
{
static void Main(string[] args)
{
C c = new C();
Console.ReadLin e();
}
}

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

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

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

public new void AddStuff()
{
Console.WriteLi ne("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******** ******@cpmsftng xa07.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 ConsoleApplicat ion4
{
class Class1
{
static void Main(string[] args)
{
C c = new C();
Console.ReadLin e();
}
}

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

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

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

public new void AddStuff()
{
Console.WriteLi ne("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
2141
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 access module that creates a large disconnected dataset from a database. I want to pass that dataset back to the calling program. And then perhaps I want to send that dataset to another class module. At first it seems that the "object oriented"...
5
1991
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 and close it when the class it disposed. I do this so that I can call mutiple methods within the class without having to open a connection each time. I implemented an IDisposable interface (and a finalize interface) but it doesn't appear to work...
10
3204
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 says that the is the public method: Eigenvalue (const TNT::Array2D< Real > &A) I write this program
7
2619
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 compiler message is this: VCSelectLibraryForm.cpp ...\Forms\QiForm.h(48) : error C3395: 'TQiForm::GetDotNetMouseButton' :
3
1544
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 "GenericDataBase.DataUtilities(cmd, "Stuff") denotes a class which is not valid in the current context. What does this mean? Here is the class that I created.... public class DataUtilities {
8
1745
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 f1(self): print 'In A.f1, calling func' self.func() def func(self):
5
2117
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 it finds a matching "Broom", i want it to return a "Broom" object to the calling program. I want the static method to call the constructor for the "Broom" class. I want this static method, (and one other called "CreateBroom") to be the only...
2
1650
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 VClass(klass): def __init__(self, *args, **kwargs): print "Calling initializer __init__ ..." klass.__init__(self, *args, **kwargs) return VClass
7
10318
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 called AssemblySettings that someone (I forget his name) had written. When the class library was finished, you deployed it to the GAC and put the configuration files in the GAC with the class library assembly. This no longer works. In fact...
0
8344
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8764
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.