Inheritance question

  • Thread starter Thread starter VJ
  • Start date Start date
V

VJ

Sorry this might really a very basic question.. not hitting me now...What am
I doing wrong???

public class Textbox2 : Textbox1
{
public Textbox2() --> gives a error No overload for method
'Textbox1' takes '0' arguments
{

}
}

public class Textbox1 : System.Windows.Forms.TextBox
{
public Textbox1(string temp)
{

}
}

VJ
 
VJ,

When you derive from a class, and declare a constructor with no
overloads, C# implicitly compiles a call to the base class constructor with
no parameters. So where you had:

public class Textbox2 : Textbox1
{
public Textbox2()
{}
}

C# compiles it into:

public class TextBox2 : TextBox1
{
public TextBox2() : base()
{}
}

However, because TextBox1 does not have a parameterless constructor, you
get a compiler error. You need to change your constructor to call the
constructor on TextBox1 which takes a parameter:

public class TextBox2 : TextBox1
{
public TextBox2 : base("some string")
{}
}

Or

public class TextBox2 : TextBox1
{
public TextBox2(string temp) : base(temp)
{}
}

Hope this helps.
 
Thanks Nicholas... Got it...

VJ

Nicholas Paldino said:
VJ,

When you derive from a class, and declare a constructor with no
overloads, C# implicitly compiles a call to the base class constructor
with no parameters. So where you had:

public class Textbox2 : Textbox1
{
public Textbox2()
{}
}

C# compiles it into:

public class TextBox2 : TextBox1
{
public TextBox2() : base()
{}
}

However, because TextBox1 does not have a parameterless constructor,
you get a compiler error. You need to change your constructor to call the
constructor on TextBox1 which takes a parameter:

public class TextBox2 : TextBox1
{
public TextBox2 : base("some string")
{}
}

Or

public class TextBox2 : TextBox1
{
public TextBox2(string temp) : base(temp)
{}
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

VJ said:
Sorry this might really a very basic question.. not hitting me now...What
am I doing wrong???

public class Textbox2 : Textbox1
{
public Textbox2() --> gives a error No overload for method
'Textbox1' takes '0' arguments
{

}
}

public class Textbox1 : System.Windows.Forms.TextBox
{
public Textbox1(string temp)
{

}
}

VJ
 
VJ said:
Sorry this might really a very basic question.. not hitting me
now...What am I doing wrong???

public class Textbox2 : Textbox1
{
public Textbox2() --> gives a error No overload for method
'Textbox1' takes '0' arguments
{

}
}

public class Textbox1 : System.Windows.Forms.TextBox
{
public Textbox1(string temp)
{

}
}

There's no empty constructor available in Textbox1.
THis one is added automatically if you don't provide a constructor
yourself, though you have provided a constructor in Textbox1, so you
have to add

public Textbox1()
{
}

to Textbox1

FB

--
 
Back
Top