Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; }
        }
    } 
like image 832
Ace Troubleshooter Avatar asked Dec 09 '22 08:12

Ace Troubleshooter


1 Answers

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

} 
like image 57
Akram Shahda Avatar answered Dec 11 '22 22:12

Akram Shahda