Late Instantiating Base Class

A

AD

Hi,
I need to instantiate a base class, but cannot do this in my inherited
class constructor as I need to calculate values for the base class,
how would I do that? (If possible)

For example:

class BaseClass
{

}

class InheritedClass : BaseClass
 
A

AD

Sorry, accidently sent the previous message

For example:

class BaseClass
{
public BaseClass(int x, int y, int z)
{
}
}

class InheritedClass : BaseClass
{
public InheritedClass(int x)
{
int y = 0;
int z = 1;
base(x, y, z); //Does not work!
}
}
 
J

Jon Skeet [C# MVP]

AD said:
I need to instantiate a base class, but cannot do this in my inherited
class constructor as I need to calculate values for the base class,
how would I do that? (If possible)

You can use a static method to calculate the values, e.g.

class InheritedClass : BaseClass
{
public InheritedClass (int x, int y)
: base (CalculateBaseConstructorArg(x, y))
{
}


static int CalculateBaseConstructorArg
(int x, int y)
{
// Some calculations in here
}
}
 

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