Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Invoking constructor in parent class
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00855612
Message ID:
00855638
Views:
12
Use the : base() syntax
public DerivedClass(object parameter) : base(parameter)
{
  Console.WriteLine("DerivedClass Constructor");
}
>I have a class with a constructor with an overload that accepts a single string argument. I have a derived class that has it's own constructor.
>
>When I instantiate the derived class, I want to invoke the base class's constructor, passing the optional argument but can't seem to figger out how this is done.
>
>Here's sample code:
>
>
>public class BaseClass
>{
>  public BaseClass(string Argument)
>  {
>    Console.WriteLine("BaseClass Constructor with argument - " + Argument);
>  }
>
>  public BaseClass()
>  {
>    Console.WriteLine("BaseClass Constructor (no arguments)");
>  }
>}
>
>public class DerivedClass : BaseClass
>{
>  public DerivedClass()
>  {
>    Console.WriteLine("DerivedClass Constructor");
>  }
>}
>
>
>Thanks
Previous
Reply
Map
View

Click here to load this message in the networking platform