Abstract class, constructors and Co

12,687

Solution 1

Constructors do not get inherited in C#. You will have to chain them manually:

public TheServer(int port) 
 : base(port)
{
}

Also, if Start is protected, you will have to create some sort of public method that calls it:

public void StartServer()
{
    Start();
}

Solution 2

Your derived class needs to add a one-parameter constructor, and delegate it to the base class:

 public TheServer(int port) : base(port) {}

Also, the Start method is protected. You'll need your own:

public void StartMe(){base.Start();}

Solution 3

These errors actually have fairly straightforward causes:

  1. You haven't defined a constructor in your derived class (TheServer). Constructors (unlike methods of course) aren't automatically inherited, so you'll need to declare constructors that match thee ones in the parent class and chain them together using something like:

    public TheServer(int port) : base(port)
    {
        // Your code here.
    }
    
    public TheServer() : base()
    {
        // Your code here.
    }
    
  2. The Start method is declared as protected in the base (UDPServer) class. Either change the access modifier in the base class to public, or figure out a way to call the method from the derived class when you need to (the latter must have been intended by the writer of UDPServer).

  3. Same reason as 1, except this is referring to the default (parameterless) constructor.

Hope that helps.

Share:
12,687
undsoft
Author by

undsoft

Updated on August 04, 2022

Comments

  • undsoft
    undsoft almost 2 years

    Well, I'm trying to reuse a portion of C# code. It's an abstract class with UDP server, which can be seen here:

    http://clutch-inc.com/blog/?p=4

    I've created a derived class like this:

    public class TheServer : UDPServer
    {
        protected override void PacketReceived(UDPPacketBuffer buffer)
        {
        }
    
        protected override void PacketSent(UDPPacketBuffer buffer, int bytesSent)
        {
        }
    }
    

    And in my app I've created an instance of the derived class like this:

    TheServer serv = new TheServer(20501);
    serv.Start();
    

    But I've got errors and I don't really understand why. Please help.

    1. 'TheProject.TheServer' does not contain a constructor that takes '1' arguments
    2. 'TheProject.UDPServer.Start()' is inaccessible due to its protection level
    3. 'TheProject.UDPServer' does not contain a constructor that takes '0' arguments
  • Smilediver
    Smilediver about 15 years
    shouldn't that be .Brings(TheBoys().All())->TheYard()
  • MemeDeveloper
    MemeDeveloper about 12 years
    Yes... but why create a needless public method "public void StartServer()" to give access to Start method (which surely is private not protected to get that error msg). Surely just change the Start method itself to be a public method if you want it to be accessible? Or am I missing some subtlety? (link no longer works)