开发者

Member defined more than once error message?

I'm using c# in vs2010, and learning oop; my first attempt at declaring the object submission in my appCode folder keeps giving me the error message that

this member defined more than once
ambiguity between Submission.SubmissionId and Submission.SubmissionId

This error throws on each variable (CustId, BroId, Coverage). I followed a model I found in a tutorial for the syntax; is that the issue? Code below:

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; }
        }
    } 


The problem is you are giving the same name to the variable and the property.

You can fix it by giving them different names:

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; }
    }


} 

Read How to best name fields and properties.


Also, you can use 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.

Here we go:

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; }

} 


Either declare your properties like this:

private int _submissionId;
public int SubmissionId 
{
    get { return _submissionId; }
    set { _submissionId = value; }
}

or you can use Auto-Implemented properties (C# 3.0 or later, which do the same thing but require less typing)

public int SubmissionId { get; set; }


You're giving your public and private members the same name. You can't do that.
You can decide to give all your private members a prefix, such as an underscore (_), as suggested here.

Also, if you're not doing anything special in your properties, @Akram Shahda's answer is a good suggestion


You named field and property with the same name, that causes the error. You whould rename field for example to submissionId. By the way, there is no need to define a field at all in this case. Use this property syntax:

public int SubmissionId { get; set; }


In this case you have a field and a property with the same name this is not allowed.

The standard backing store implementation would look like this.

private int _CustID;
public int CustId
{
     get { return _CustID; }
     set { _CustID= value; }
}

If you are just using Properties with no get or set logic take advantage of the automatic properties.

public int CustID {get;set;}


Your private and public members name is same.

change your private menbers name as

 int _submissionId;
 int _custId;
 int _broId;
 int _coverage; 

if you are using visual studio 2008 or later try

 public int SubmissionId { get; set; }
  public int CustId { get; set; }
  public int BroId { get; set; }
  public int Coverage { get; set; } 


Ya, any how in the code u have declared each of SubmissionId, CustId, BroId, Coverage twice right. Why is so? u can directly declare a property below. Remove the first declaration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜