Calling virtual function from constructor


C

Claire

Hi,
I have a virtual property getter/setter in my ancestor class.
It's overridden in inherited classes to validate that the value passed in is
within max/min bounds for that inherited class.
The constructors of the inherited classes have a parameter that initializes
this value.
eg
public childClass(int TimeMins)
{
this.TimeMins = TimeMins;
}

I'm being warned (by ReSharper) that I'm calling a virtual method from the
constructor.

Does anyone have any idea how I can get around this if its not good practice
please?
 
Ad

Advertisements

J

Jeroen de Zeeuw

If you use a backing field property, the resharper warning goes away. Not
sure though if this is the right solution.

so, something like;

class childClass
{

int _timeMins;

public childClass(int TimeMins)
{
_timeMins = TimeMins;
}

public int TimeMins
{
get { return _timeMins; }
}

}

Hope it helps. Jeroen.
 
C

Claire

If you use a backing field property, the resharper warning goes away. Not
sure though if this is the right solution.

No, that doesnt help. The validations in the inherited classes virtual "set"
methods can be pretty complex.

That's why I needed to make them virtual.
But thank you for answering jeroen :)
 
M

miher

Hi,

It's not a good idea to call anything that is virtual from constructors,
since virtual methods might be using not-yet initalized state from the
object. See an example here:
http://msdn.microsoft.com/en-us/library/ms182331(VS.80).aspx

To prevent this first fully construct the object and then set the property.
It can be done in really compact way using the new object initalization
format :
var v = new childClass() {TimeMins = someValue};
(If You dont want to expose this property try introducing for example a
factory (http://msdn.microsoft.com/en-us/library/ms954600.aspx))

Hope You find this useful.
-Zsolt
 
G

Gareth Erskine-Jones

Hi,
I have a virtual property getter/setter in my ancestor class.
It's overridden in inherited classes to validate that the value passed in is
within max/min bounds for that inherited class.
The constructors of the inherited classes have a parameter that initializes
this value.
eg
public childClass(int TimeMins)
{
this.TimeMins = TimeMins;
}

I'm being warned (by ReSharper) that I'm calling a virtual method from the
constructor.

Does anyone have any idea how I can get around this if its not good practice
please?

You could declare the childClass as sealed.
 
Ad

Advertisements

B

Bob Milton

Claire,
The way around this is to put the actual validation code in a separate,
non-virtual method. Then call that method from your virtual override AND at
the end of your constructor. That way you will get the correct code
executed.
Bob
 
Ad

Advertisements

I

Ignacio Machin ( .NET/ C# MVP )

Hi,
I have a virtual property getter/setter in my ancestor class.
It's overridden in inherited classes to validate that the value passed inis
within max/min bounds for that inherited class.
The constructors of the inherited classes have a parameter that initializes
this value.
eg
public childClass(int TimeMins)
{
    this.TimeMins = TimeMins;

}

I'm being warned (by ReSharper) that I'm calling a virtual method from the
constructor.

Does anyone have any idea how I can get around this if its not good practice
please?

You should not call any virutal method in a constructor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top