简体   繁体   English

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

[英]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: classe如下:

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? 有人可以用我的方式指出错误,并简要解释一下我做错了什么以及为什么会抛出这个错误?

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. MSDN是一个很好的起点,但快速谷歌搜索将为您找到许多深度信息来源。


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. 似乎奇怪的是它总是与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) (如果你知道这一切,只是不明白错误,那么我道歉)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#“必须声明一个主体,因为它没有被标记为抽象、外部或部分” - C# “must declare a body because it is not marked abstract, extern, or partial” 必须声明一个主体,因为它没有标记为抽象外部或局部 - must declare a body because it is not marked abstract extern or partial C#必须声明一个主体,因为它没有被标记为抽象,外部或部分 - C# must declare a body because it is not marked abstract, extern, or partial 获取必须声明一个没有标记为抽象,外部或局部的主体 - get must declare a body it is not marked abstract, extern, or partial 错误:必须声明一个主体,因为它没有被标记为抽象,外部或局部 - Error: must declare a body because it is not marked abstract, extern, or partial struct Cat(string,string,bool,bool)&#39;必须声明一个主体,因为它没有被标记为abstract,extern或局部 - struct Cat(string, string, bool, bool)' must declare a body because it is not marked abstract, extern, or partial 我没有解决“必须声明一个主体,因为它没有被标记为抽象,外部或部分”的问题吗? - i don't solve “must declare a body because it is not marked abstract, extern, or partial” problem? DispatcherTimer.Start()&#39;必须声明一个主体,因为它在MVC4中未标记为抽象,外部或部分 - DispatcherTimer.Start()' must declare a body because it is not marked abstract, extern, or partial in MVC4 “warp(Vector3)”必须声明一个主体,因为它没有标记为抽象、外部或部分 - 'warp(Vector3)' must declare a body because it is not marked abstract, extern, or partial C# 错误:声明一个主体,因为它没有标记为抽象、外部或部分 - C# errors: declare a body because it is not marked abstract, extern, or partial
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM