views:

66

answers:

2

Hello all.

I'm a tool, tired and completely out of my depth!!

I have created the following class based on some old code so one has kindly given me. However, I cannae get past the error as described in the title.

The classe 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 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?

Any help gratefully received.

+1  A: 

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)

Martin Harris
you mean he needs a body for the /abstract/ method "int Compare(T x, T y)...? ;)
AllenG
@AllenG - No, he doesn't mean that. You can't have an abstract function with a body. That's the whole reason for having an abstract method, to force subclasses to implement their own.
fletcher
@fletcher - I mistyped the answer first to include the Compare method as an error not noticing that it was abstract. Fortunately I fixed it within the edit window and now the only evidence of my foolishness is Allen's comment and this admission.
Martin Harris
Ricardo Deano
Hello Martin - you were also correct in saying that this class wouldn't work.I was given the wrong piece of code to dissect. I thought it looked a bit thin on the ground for what it was supposed to do!
Ricardo Deano
+8  A: 

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.

Mark Rushakoff
Meaning, you need to add a block of code in curly brackets to do something. When some code calls ObjectComparer and passes it comparefield and direction strings, it probably needs to create an object and set those properties.
DOK