必须声明一个主体,因为它没有标记为抽象、外部或部分

发布于 2024-09-11 19:11:28 字数 917 浏览 5 评论 0原文

我创建了以下课程。但是,我无法克服错误:

必须声明一个主体,因为它没有标记为抽象、外部或部分

类如下:

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

有人可以指出我的方式中的错误,并给我一个简短的解释,说明我做错了什么以及为什么会抛出此错误?

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.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卸妝后依然美 2024-09-18 19:11:31

您需要添加一个方法体,因为

public ObjectComparer(string compareField, string direction);

我建议您对抽象类进行一些研究。 MSDN 是一个很好的起点,但是快速谷歌搜索将为您找到许多深入信息的来源。


既然问题已经得到合理的回答,我将添加一些额外的注释,因为您给出的代码看起来很糟糕。

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

这些似乎是在比较器中使用的一对奇怪的命名空间(尤其是第二个),这个类是来自一个更大的文件,而您还没有获得全部内容,还是它只是遗留下来的遗留代码?


public ObjectComparer(string compareField, string direction);

我猜构造函数应该设置这样的属性?

public ObjectComparer(string compareField, string direction)
{
    CompareField = compareField;
    Direction = direction;
}

public string Direction
{ 
    get { return compareField; } 
    set { compareField = value;} 
}

我认为这应该有它自己的支持领域。看起来很奇怪,它总是与 CompareField 相同。


我并不是有意无礼,但仅仅克服这个错误并不能让这门课成功。您确实需要了解您正在尝试做什么以及这样的课程如何帮助您做到这一点。 (如果你知道这一切但只是不明白这个错误,那么我深表歉意)

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)

过期情话 2024-09-18 19:11:30

您已经声明了没有主体的构造函数:

public ObjectComparer(string compareField, string direction);

如果您不希望构造函数执行任何操作,您仍然可以在那里放置一个空主体 ({ })。

附带说明一下,拥有一个带有公共构造函数的抽象类是没有意义的 - 构造函数应该受到保护,因为构造函数无论如何都只能被从它派生的类“调用”。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文