Inheritance

G

Guest

Hi,
I have a class (ClassA) which inherits another class (ClassB). I need to
instantiate ClassB using a value determined in ClassA's constructor - ie can
I have a constructor as follows;
public ClassA()
{
//Do various things
base(iMyValue);
}
....instead of...
public ClassA() : base(iMyValue)
{
//Do various things
}

Thanks in advance,
Al.
 
M

Mantorok

Al

AFAIK this cannot be achieved, what you would have to do is create a
protected "initialiser" method in ClassA, then rather than pass in the value
to ClassA's constructor, pass the value into the method, ClassA would also
use this method as well, rather than initialising in the constructor itself.

Kev
 
M

Mantorok

Sorry, ClassB would have the initialiser method and ClassA would call it,
sorry got mixed up there.

Kev
 
J

Joanna Carter [TeamB]

"Al" <[email protected]> a écrit dans le message de (e-mail address removed)...

| I have a class (ClassA) which inherits another class (ClassB). I need to
| instantiate ClassB using a value determined in ClassA's constructor - ie
can
| I have a constructor as follows;
| public ClassA()
| {
| //Do various things
| base(iMyValue);
| }
| ...instead of...
| public ClassA() : base(iMyValue)
| {
| //Do various things
| }

This has just been answered. Use a private static method :

public class ClassA
{
private static int GetInitialValue()
{
return // initial value
}

public ClassA() : base(GetInitialValue())
{
//Do various things
}
}

Joanna
 

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