views:

1718

answers:

8

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

Thanks for the answers.

A: 

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.

Otávio Décio
+6  A: 

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();}
John Saunders
+3  A: 

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();
}
DrJokepu
A: 
  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.

Kurt Schelfthout
A: 
public class TheServer 
{   
    public TheServer():base(port) {
    }    
}

var myServer = new TheServer(1337).Brings().All().The().Boys().to().The().Yard()
Chad Grant
shouldn't that be .Brings(TheBoys().All())->TheYard()
Sam Hasler
A: 

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();
}
Eoin Campbell
+1  A: 

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.

Noldorin
Downvote because ...?
Noldorin
A: 

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.

Elliot