views:

8395

answers:

9

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!

A: 

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

Nikki9696
He is attempting to utilize auto generation of the methods, a new feature in C# 3.0
Guvante
MSDN has an example that does not use abstract: http://msdn.microsoft.com/en-us/library/bb384054.aspx
Chris Ammerman
the only time you have to use abstract is if you are targeting .net 2.0
Russ Bradberry
+1  A: 

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.

James Curran
+1  A: 

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.

Darren Kopp
A: 

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)

MADMap
+3  A: 

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.

Mike Comstock
Why should this work? I've used auto-properties in VS2008 targeting .NET 2.0 before. In fact, I just tested it AGAIN to make sure, and it works fine.
Chris Ammerman
To be honest, I don't think it should matter. It should work fine when targeting 2.0, since automatic properties are just a shorthand that the compiler expands into regular properties anyway.
Mike Comstock
A: 

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.

Chris Ammerman
+4  A: 

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>
R.L.
Or maybe create a web.config file :)
HasanGursoy
A: 

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.

CodeMonkey
A: 

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.

Maurice Lenz