.NET property generating “must declare a body because it is not marked abstract or extern” compilation error

后端 未结 9 1400
离开以前
离开以前 2021-02-13 20:03

I have a .NET 3.5 (target framework) web application. I have some code that looks like this:

public string LogPath { get; private set; }
public string ErrorMsg          


        
相关标签:
9条回答
  • 2021-02-13 20:31

    It is, as long as you put abstract in front, or implement the methods.

    public abstract string LogPath { get; private set; }
    public abstract string ErrorMsg { get; private set; }
    

    See http://forums.asp.net/t/1031651.aspx

    0 讨论(0)
  • 2021-02-13 20:32

    This error can also happen if you are using CodeFile="MyControl.ascx.cs" in your MyControl.ascx instead of CodeBehind="MyControl.ascx.cs".

    In case of CodeFile, the 2.0 compiler tries to recompile the page, even if you have a WebProject instead of a WebSite and of course - does fail.

    Changing the attribute name to CodeBehind fixed the problem in my case.

    0 讨论(0)
  • 2021-02-13 20:33

    The syntax is valid. And you can set different access modifiers. You aren't on an Interface are you? And the class these are in isn't abstract is it?

    Also, doesn't matter what v. of the framework you target because this is a compiler feature. VS2008 will implement the property w/ backing stores for you.

    0 讨论(0)
  • 2021-02-13 20:34

    That error should not be coming from the code you posted. According to MSDN, you've done this right: http://msdn.microsoft.com/en-us/library/bb384054.aspx

    Hence I would recommend you re-check the error message, and where the compiler says the error is coming from. The text of the message you posted did not include a reference to properties, and there is a similar message for functions... Anything that is missing an implementation and not on an interface or marked abstract or extern can generate this error.

    The auto-property is a feature of the C# 3.0 language/compiler. If you are using VS 2008, it should work even if you are targeting .NET 2.0. I JUST tested it to make sure.

    0 讨论(0)
  • 2021-02-13 20:38

    Your code is valid - it should work fine. Go in to the property pages of your project and make sure that the "Target Framework" is .NET 3.0 or 3.5.

    0 讨论(0)
  • 2021-02-13 20:39

    Where do you define this Properties? Directly in the as*x file or in the codeBehind? (I don't think that can be a reason, but if the build-Target is .NET 3.5 I can't see anything else)

    0 讨论(0)
提交回复
热议问题