Inheritance with a constructor that takes arguments problem

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Messages
1,031
C#:
class Inherit : c1
{
  Inherit(){} // calls c1() constructor but not c1(string argument)
//which messes things up.
//How would I call the constructor with an argument in the base class?
}


class c1 : UserControl
{
  public c1(){}
  public c1(string argument)
  {
    // argument is used here and is important!
  }
}
 
Instead of doing the initialization inside the constructor of the
base class, create a protected or public method in the base
class that does the initialization, and then you can call it in both
the regular constructor and the inheritor's constructor.

C#:
class Inherit : c1
{
  Inherit()
  {
    base.InitClass("whatever");
  }
}


class c1 : UserControl
{
  public c1(){}
  public c1(string argument)
  {
    InitClass(argument);
  }
  
  protected void InitClass(string argument)
  {
    // Initialize the class with the argument in this sub
  }
}

Does this solve your problem?
 
The "correct" way to do what you're after is this:

[CS]
class Inherit : c1
{
Inherit() : base("whatever")
{
}
}


class c1 : UserControl
{
public c1() { }

public c1(string argument)
{
// Initialize the class with the argument in this sub
}
}
[/CS]
 
Back
Top