Detect Execution in Constructor

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a protected method that I only want it to be called from code in
constructor or from methods called by constructor. If it is not called
directly or indirectly from constructor, I want to throw an exception. Is
there anyway to implement this?

Thanks in advance!
 
Create a "readonly" private bpp;eam member in your class. Initialize it
to true:

private readonly bool _inConstructor = true;

When your constructor finishes running, set it to false:

this._inConstructor = false;

Since a readonly member can be set only in a constructor and nowhere
else, you're guaranteed that if _inConstructor is true, you're running
the constructor.
 
Hmm... thinking this over, it works fine until you're running the
constructor of a child class, at which point _inConstructor will be
false.

To make this work for child classes, too, you would have to make
_inConstructor protected, and at the start of every child class
constructor, say:

this._inConstructor = true;

and then

this._inConstructor = false;

at the end.

Not a very satisfactory solution, but is the only other solution to
walk the stack (yuck!)? Anyone?
 
Gniluy said:
I have a protected method that I only want it to be called from code in
constructor or from methods called by constructor. If it is not called
directly or indirectly from constructor, I want to throw an exception. Is
there anyway to implement this?

In addition to the other replies, I wanted to mention that calls into
virtual code from constructors are regarded by many as a very dangerous
practice. Try googling for "call virtual constructor", that'll find you
a lot of information on the topic.



Oliver Sturm
 
Oliver Sturm said:
In addition to the other replies, I wanted to mention that calls into
virtual code from constructors are regarded by many as a very dangerous
practice. Try googling for "call virtual constructor", that'll find you
a lot of information on the topic.

It's generally a bad idea if it's a "normal" virtual method - i.e. one
which wouldn't be expected to be called outside a constructor. If a
virtual method documentation explicitly states that it will be called
within a constructor, it's considerably easier to handle - at least in
C#. In Java it's harder, as instance variable initialisers in Java are
executed *after* the superclass constructor returns.
 
Jon said:
It's generally a bad idea if it's a "normal" virtual method - i.e. one
which wouldn't be expected to be called outside a constructor. If a
virtual method documentation explicitly states that it will be called
within a constructor, it's considerably easier to handle - at least in
C#. In Java it's harder, as instance variable initialisers in Java are
executed *after* the superclass constructor returns.

Personally, if I call into virtual code from a constructor, I always
comment that on the method I'm calling as well as on the call in the
constructor itself. I also try to avoid doing it in the first place,
because I assume I will one day forget to consider the consequences and
be bitten by it :-)

Anyway, it's important to know about the specific problems in such a
scenario, that's why I thought I'd mention it.



Oliver Sturm
 
Thanks a lot, Bruce.

But readonly field can not be assigned to a new value in the contructor of
derived class.
 
Thanks a lot, guys. Really appreciated.

I have a protected method that is not a virtual method. I want the derived
class to call it from and only from contructor. It is used to initialize some
values when an object is created. I do not expect this method to be
overridden.
 
Hi Oliver,

I am aware of the problems doing this in C++, but are there pitfalls also in
C#? I was under the impression that this problem was fixed in C#.

Thanks,

- Tom
 
Gniluy said:
I have a protected method that is not a virtual method. I want the derived
class to call it from and only from contructor. It is used to initialize some
values when an object is created. I do not expect this method to be
overridden.

Ah, sorry. I jumped to conclusions here because you said the method was
protected.


Oliver Sturm
 
Tom said:
I am aware of the problems doing this in C++, but are there pitfalls also in
C#? I was under the impression that this problem was fixed in C#.

The problems are different than they were in C++, because the order of
initialization is different.

In C++, the object is constructed from the base on up, so when you call
a virtual method from a base class constructor, you end up calling the
base class's own implementation of that method, because the "upper
layers" of the class you are constructing are not yet there at that point.

In C#, the object is constructed the other way round, or for most
intents and purposes it would be correct to say that it's constructed in
one go. So when calling a virtual method from a base class, you actually
end up in the derived class's method. Whether this is a good idea to use
depends on what exactly you do in that derived method: you must be aware
that any instance fields of the derived class haven't been initialized
yet and that the derived class's own constructor code has not yet been
run. As Jon and I discussed, sometimes this is a good thing to be able
to use, but it's important that you are aware of the fact that a virtual
method is going to be called from a base class constructor when you
override it in a derived class.


Oliver Sturm
 
Back
Top