C# 属性访问器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1161732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Property Accessor Error
提问by
I have to write an address book program in C# 2008. It is supposed to ask the user for the person's Name, Email, and Favorite Color (only by the colors in the enumeration). Then it is supposed to save the contacts for future reference.
我必须在 C# 2008 中编写一个地址簿程序。它应该向用户询问此人的姓名、电子邮件和最喜欢的颜色(仅通过枚举中的颜色)。然后它应该保存联系人以备将来参考。
This is the code the produces an error.:
这是产生错误的代码。:
class Contact
{
string Name; //This string represents the person's Name.
string Email; //This string represents the person's Email.
System.Drawing.KnownColor Favoritecolor
{
get;
}
static void Request()
// This function requests the user to type in information about the person.
{
Console.WriteLine("Please enter the person's name, e-mail, and favorite color");
Console.Write; string Name; string Email; ;
Console.ReadLine();
}
}
The error is:
错误是:
'Lab02.Program.Contact.Favoritecolor': property or indexer must have at least one accessor
采纳答案by Brandon
System.Drawing.KnownColor Favoritecolor
{
get;
set;
}
Right now you have a get on the FavoriteColor property, but no where is it ever set, so it can never return an actual value.
现在你有一个对FavoriteColor 属性的get,但是没有在哪里设置它,所以它永远不会返回一个实际值。
If you want to implement an auto property, you need to add a set. Otherwise create a backing field and return that.
如果要实现自动属性,则需要添加一个集合。否则创建一个支持字段并返回它。
private System.Drawing.KnownColor _favoriteColor = someValue;
System.Drawing.KnownColor Favoritecolor
{
get { return _favoriteColor; }
}
回答by Charlie
Your Favoritecolor property needs to have both a get and a set accessor. Like this:
您的 Favoritecolor 属性需要同时具有 get 和 set 访问器。像这样:
System.Drawing.KnownColor Favoritecolor
{
get;
set;
}
I think something like this is more what you are going for:
我认为这样的事情更适合你:
class Program
{
static void Main(string[] args)
{
Contact contact = new Contact();
Console.WriteLine("Please enter the person's name:");
contact.Name = Console.ReadLine();
Console.WriteLine("Please enter the person's e-mail address:");
contact.Email = Console.ReadLine();
while (contact.Favoritecolor == 0)
{
Console.WriteLine("Please enter the person's favorite color:");
string tempColor = Console.ReadLine();
try
{
contact.Favoritecolor = (System.Drawing.KnownColor)(Enum.Parse(typeof(System.Drawing.KnownColor), tempColor, true));
}
catch
{
Console.WriteLine("The color \"" + tempColor + "\" was not recognized. The known colors are: ");
foreach (System.Drawing.KnownColor color in Enum.GetValues(typeof(KnownColor)))
{
Console.WriteLine(color);
}
}
}
}
class Contact
{
//This string represents the person's Name.
public string Name { get; set; }
//This string represents the person's Email.
public string Email { get; set; }
public System.Drawing.KnownColor Favoritecolor
{
get;
set;
}
}
}
You don't even need your Colors enumeration, because you are using System.Drawing.KnownColor as your property type. So you can take that out entirely.
您甚至不需要 Colors 枚举,因为您使用 System.Drawing.KnownColor 作为您的属性类型。所以你可以把它完全去掉。
回答by ist_lion
You may want to put something in your main method , since we'll it doesn't point to anything and therefore nothing will load when you run the app
你可能想在你的主方法中放一些东西,因为我们不会指向任何东西,因此当你运行应用程序时不会加载任何东西