我在 vs2010 中使用 c#,并正在学习 oop;我第一次尝试在我的 appCode 文件夹中声明对象提交时一直给我错误消息
this member defined more than once
ambiguity between Submission.SubmissionId and Submission.SubmissionId
此错误会引发每个变量(CustId、BroId、Coverage)。我遵循了在语法教程中找到的模型;那是问题吗?代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class Submission
{
int SubmissionId;
int CustId;
int BroId;
int Coverage;
//Array Product[] products;
public Submission() {}
public int SubmissionId
{
get { return SubmissionId; }
set { SubmissionId = value; }
}
public int CustId
{
get { return CustId; }
set { CustId = value; }
}
public int BroId
{
get { return BroId; }
set { BroId = value; }
}
public int Coverage
{
get { return Coverage; }
set { Coverage = value; }
}
}
最佳答案
问题是您为变量和属性赋予了相同的名称。
您可以通过给它们不同的名称来修复它:
public class Submission
{
public Submission() {}
private int submissionId;
public int SubmissionId
{
get{ return this.submissionId; }
set{ this.submissionId = value; }
}
private int custId ;
public int CustId
{
get{ return this.custId ; }
set{ this.custId = value; }
}
private int broId ;
public int BroId
{
get{ return this.broId ; }
set{ this.broId = value; }
}
private int coverage;
public int Coverage
{
get{ return this.coverage; }
set{ this.coverage= value; }
}
}
阅读How to best name fields and properties .
此外,您可以使用 Auto-Implemented Properties :
In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects.
开始吧:
public class Submission
{
public Submission() {}
public int SubmissionId { get; set; }
public int CustId { get; set; }
public int BroId { get; set; }
public int Coverage { get; set; }
}
关于c# - 成员定义了不止一次的错误信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6134148/