Inheritance

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
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
 
Sorry, ClassB would have the initialiser method and ClassA would call it,
sorry got mixed up there.

Kev
 
"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
 
Back
Top