Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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!
 }
}

C#
  • *Experts*
Posted

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.

 

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?

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • 2 weeks later...
Posted

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]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...