Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract class, constructors and Co

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
like image 796
undsoft Avatar asked May 01 '09 13:05

undsoft


1 Answers

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();
}
like image 97
Tamas Czinege Avatar answered Nov 06 '22 19:11

Tamas Czinege