Must declare a body because it is not marked by abstract, external or partial

I created the following class. However, I can not get past the error:

Must declare a body because it is not marked by abstract, external or partial

The layer is as follows:

using System; using System.Collections.Generic; 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 a mistake in my ways, and also give me a brief explanation of what I'm doing wrong, and why is he throwing this error?

+4
source share
2 answers

You need to add a method body for

 public ObjectComparer(string compareField, string direction); 

I suggest you do some research on abstract classes. MSDN is a good starting point, but a quick Google search will find many sources of detailed information.


Since the question was reasonably answered, I will add additional comments, since the code you received looks pretty broken.

 using System.Web; using System.Runtime.CompilerServices; 

It seems that this is an odd pair of namespaces to be used in comparison (especially the second one), is this class from a larger file and you donโ€™t have it, or is it just left over the old code?


 public ObjectComparer(string compareField, string direction); 

I assume that the constructor should tune 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 its own support field. It seems strange that it will always be the same as CompareField.


I donโ€™t want to be rude, but just passing this error will not make this class work. You really need to understand exactly what you are trying to do, and how such a class can help you do it. (If you know all this and just did not understand the mistake, I apologize)

+2
source

You declared a constructor without a body:

 public ObjectComparer(string compareField, string direction); 

If you don't want the constructor to do nothing, you can still put an empty body ( { } ) in it.

As a side note, it makes no sense to have an abstract class with an open constructor - the constructor must be protected , because the constructor can only be โ€œcalledโ€ by classes that come from it anyway.

+8
source

All Articles