gpt4 book ai didi

c# - 成员定义了不止一次的错误信息?

转载 作者:太空狗 更新时间:2023-10-30 00:06:03 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com