Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, October 3, 2006 8:07 AM
When I write the code below I'm getting the error: "must declare a body because it is not marked abstract or extern" for get and set accesors. What am I doing wrong?
Thanks a lot
using System;
namespace Dados
{
public class Manutencao : IDados
{
public Manutencao()
{ }
public string Nome { get; set;}
public string Idade { get; set;}
}
}
All replies (12)
Tuesday, October 3, 2006 10:46 AM âś…Answered
Hey no problem we all have to learn. Basically an interface is just that just a set of properties and methods that tell you how to access a class. So they naturally come with no code. However when you impliment an interface you also have to impliment the code to define that interface. For example:
using System;
namespace Dados
{
public class Manutencao : IDados
{
private string _nome;
private string _idade;
public Manutencao() { }
public string Nome
{
get { return _nome; }
set { _nome = value; }
}
public string Idade
{
get { return _idade; }
set { _idade = value; }
}
}
}
Tuesday, October 3, 2006 10:16 AM
The problem is that you declared no body for the get and set.
using System;
namespace Dados
{
public abstract class Manutencao : IDados
{
public Manutencao() { }
public abstract string Nome { get; set; }
public abstract string Idade { get; set; }
}
}
Tuesday, October 3, 2006 10:35 AM
Thanks a lot nberardi for reply and sorry for my newbie question but why did you add abstract keyword and what would be the content of that IDados interface that is being inheritanced?
Thursday, March 19, 2009 11:43 AM
just my 2 cents if anyone is looking at this. If you are using Visual Studio 2008, You can target your compiler to compile at 3.5 framework which supports Automatic Properties. If you are using 2.0 then you will need to have abstract class interface.
Thursday, March 19, 2009 4:31 PM
just my 2 cents if anyone is looking at this. If you are using Visual Studio 2008, You can target your compiler to compile at 3.5 framework which supports Automatic Properties. If you are using 2.0 then you will need to have abstract class interface.
This is incorrect. Automatic properties are a function of the compiler, not of the framework you're targeting. VS2k8 will still work with automatic properties when targeting the .Net 2.0 runtime. However VS2k5 will not give you automatic properties.
Saturday, March 21, 2009 7:17 PM
There is nothing wrong with the code you have posted, so far. I supsect that the interface IDados has some other members declared that need to be implemented in your Manutencao class. The easy way to get them implemented is to position your cursor on IDados (in this file, not in the declaration of IDados) and press Alt-Shift-F10 and then Enter. This will give default implementations for any parts of the interface which have not been implemented in your concrete class.
Tuesday, March 31, 2009 4:41 PM
Error is not because of wrong code but its because of missing code. I think Interface IDados will be having few methods which has not been implemented in the class. Once you do that the error will vanish. :)
Wednesday, April 1, 2009 3:09 AM
What am I doing wrong?
You are apparently trying to compile C# version 3.0 code (which is supported by Visual Studio 2008) with a C# version 2.0 compiler (which is what is included in Visual Studio 2005).
For your compiler, you'll have to write:
private string _nome;
public string Nome
{
get { return _nome; }
set { _nome = value; }
}
private string _idade;
public string Idade
{
get { return _idade; }
set { _idade= value; }
}
More to write - but the same code is actually produced in the end.
To ease the burden of your fingers, try typing 'prop' <TAB><TAB> when you want to insert a property like this. <TAB> represents a single press on the tabulator key, usually identified with a right and left arrow, usually placed at the left edge of the keyboard.
Wednesday, April 1, 2009 3:22 AM
What am I doing wrong?
You are apparently trying to compile C# version 3.0 code (which is supported by Visual Studio 2008) with a C# version 2.0 compiler (which is what is included in Visual Studio 2005).
For your compiler, you'll have to write:
private string _nome;
public string Nome
{
get { return _nome; }
set { _nome = value; }
}
private string _idade;
public string Idade
{
get { return _idade; }
set { _idade= value; }
}
More to write - but the same code is actually produced in the end.
To ease the burden of your fingers, try typing 'prop' <TAB><TAB> when you want to insert a property like this. <TAB> represents a single press on the tabulator key, usually identified with a right and left arrow, usually placed at the left edge of the keyboard.
Absolutely agree with u!!!
Friday, April 3, 2009 7:36 AM
What I will say is probably you need to write the code as:
// Your code
public string Nome { get; set;}
public string Idade { get; set;}
// New code
Using System.Runtime.CompilerServices;
public string Nome { [CompilerGenerated]get; [CompilerGenerated]set;}
public string Idade { [CompilerGenerated]get; [CompilerGenerated]set;}
Regards,
Soumen, India
Friday, May 15, 2009 6:22 AM
change compiler settings in web.config file as following;
<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>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">
<providerOption name="CompilerVersion" value="v3.5" />
<providerOption name="OptionInfer" value="true" />
<providerOption name="WarnAsError" value="false" />
</compiler>
</compilers>
</system.codedom>
Thursday, January 9, 2014 8:10 AM
Great.
Solved my problem,
Thanks.