Must declare a body becase it is not marked abstract, extern or partial
I have created the following class. However, I cannot get past the error:
Must declare a body becase it is not marked abstract, extern or partial
The classe is as follows:
using System;
using System.Collections.Gen开发者_StackOverfloweric;
using System.Linq;
using System.Web;
using System.Runtime.CompilerServices;
namespace VDSORDAL
{
public abstract class ObjectComparer<T> : IComparer<T>
{
public ObjectComparer(string compareField, string direction);
private string compareField;
public string CompareField
{
get { return compareField; }
set { compareField = value; }
}
public string Direction
{
get { return compareField; }
set { compareField = value;}
}
public abstract int Compare(T x, T y);
}
}
Can someone point out the error in my ways and also give me a brief explanation as to what I am doing wrong and why it is throwing this error?
You have declared the constructor without a body:
public ObjectComparer(string compareField, string direction);
If you don't want the constructor to do anything, you can still put an empty body ({ }
) there.
As a side note, it doesn't make sense to have an abstract class with a public constructor -- the constructor should be protected, because the constructor can only be "called" by classes deriving from it anyway.
You need to add a method body for
public ObjectComparer(string compareField, string direction);
I'd suggest that you do some research into abstract classes. MSDN is a good starting point, but a quick Google search will find you many sources of in depth information.
Since the question has been reasonably answered I'll add some extra comments as the code you have been given looks quite broken.
using System.Web;
using System.Runtime.CompilerServices;
These seem to be a odd couple of namespaces to be using in a comparer (especially the second), is this class from a larger file and you haven't got all of it, or is it just left over legacy code?
public ObjectComparer(string compareField, string direction);
I'm guessing the constructor should be setting up the properties like this?
public ObjectComparer(string compareField, string direction)
{
CompareField = compareField;
Direction = direction;
}
public string Direction
{
get { return compareField; }
set { compareField = value;}
}
I think this should have it's own backing field. Seems strange that it will always be the same as CompareField.
I don't mean to be rude, but just getting past that error won't make this class work. You really need to understand what it is you are trying to do and how a class like this may help you do it. (If you know all this and just didn't understand the error then I apologise)
精彩评论