undsoft
undsoft

Reputation: 818

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

Upvotes: 7

Views: 12727

Answers (8)

Elliot
Elliot

Reputation: 1286

And, for what it's worth, I'd recommend using:

UDPServer serv = new TheServer(20501);
serv.start();

Or, perhaps even more generic, Server. Depends on what methods you need to call on serv.

Upvotes: 0

Noldorin
Noldorin

Reputation: 147240

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.

Upvotes: 2

Eoin Campbell
Eoin Campbell

Reputation: 44268

You'll need to post the code to your Abstract class but at a complete guess,

You've got a ctor in your UDPServer class that you haven't implemented in your TheServer Class... You need something like...

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

And you've also forgotten to override the Start() method in your TheServer class but its marked as private in the underlying class... Your underlying class should have something like...

//In UDPServer
protected void Start()
{
   //Code to start
}

//In TheServer
protected void StartTheServer()
{
   base.Start();
}

Upvotes: 0

Chad Grant
Chad Grant

Reputation: 45382

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

var myServer = new TheServer(1337).Brings().All().The().Boys().to().The().Yard()

Upvotes: 0

Kurt Schelfthout
Kurt Schelfthout

Reputation: 8980

  1. and 3.

Add a constructor to TheServer that calls the base constructor (of UDPServer); something like this:

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

2 Check out the method Start on UDPServer: it is protected. This means that only subclasses of that class can call it.

Upvotes: 0

Tamas Czinege
Tamas Czinege

Reputation: 121294

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();
}

Upvotes: 13

John Saunders
John Saunders

Reputation: 161773

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();}

Upvotes: 9

Otávio Décio
Otávio Décio

Reputation: 74250

You didn't define a TheServer constructor with one argument so you can't call TheServer(20501); you didn't define a zero arguments constructor for UDPServer but you defined one with one argument. Your two methods in TheServer are protected, hence the error on #2.

Upvotes: 0

Related Questions