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

StackOverflow https://stackoverflow.com/questions/95683

  •  01-07-2019
  •  | 
  •  

Question

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 { get; private set; }

It's giving me this compilation error for these lines:

"must declare a body because it is not marked abstract or extern."

Any ideas? My understanding was that this style of property was valid as of .NET 3.0.

Thanks!


The problem turned out to be in my .sln file itself. Although I was changing the target version in my build options, in the .sln file, I found this:

TargetFramework = "3.0"

Changing that to "3.5" solved it. Thanks, guys!

Was it helpful?

Solution

add to web.config

<system.codedom>
    <compilers>
        <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">
            <providerOption name="CompilerVersion" value="v3.5" />
            <providerOption name="WarnAsError" value="false" />
        </compiler>
    </compilers>
</system.codedom>

OTHER TIPS

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.

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.

You are correct; that style is allowed.

I'd look into the standard assemblies referenced. I'm not sure which you'd need to get that to compile, but I figure somewhat you're pointing to the .Net v2.0 version of csc.exe.

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.

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.

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)

This also happens on a raw web site project where there was no web.config generated.

Although the solution file said 3.5, .Net needed the web.config to state it also to recognize. I ran debug allowing it to create a webconfig, and all was working.

So it is like the answer provided, but just make sure you have one.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top