1

Researched this error and some have said it's a bug but when I used some of their suggestions it didn't fix the problem. What should I do?

**Code

/// Indicates if the profiles has been added.

public Boolean _isNew
{
    get { return _isNew; }
    set { _isNew = value; }
}

/// Indicates if any of the fields in the class have been modified

public Boolean _isDirty
{
    get { return _isDirty; }
    set { _isDirty = value; }
}


 //Gets and Sets delimiterChar 

         public Char _delimiterChar
     {
    get { return _delimiterChar; }
    set  { _delimiterChar = value;}

    }

Error**

Ambiguity between 'ConsoleApplication3.ProfileClass.isNew'and 'ConsoleApplication3.ProfileClass.isNew

Ambiguity between 'ConsoleApplication3.ProfileClass.isDirty'and 'ConsoleApplication3.ProfileClass.isDirty

Ambiguity between 'ConsoleApplication3.ProfileClass._delimiterChar'and 'ConsoleApplication3.ProfileClass._delimiterChar
Rory
  • 29
  • 2
  • 1
    Seems like you've given some properties the same name as their backing field. They have to be different. Traditionally the field name would begin with an underscore and be in camelCase, while the property name would be in PascalCase with no prefix. – John Wu Aug 19 '19 at 22:38

2 Answers2

1

The code you have posted will cause recursion and eventual stackoverflow. You're trying to set property inside the property setter. You either need a backing field or automatic properties to achieve what you're doing. Something like:

private bool _isNew;
public Boolean IsNew
{
    get { return _isNew; }
    set { _isNew = value; }
}

or

public Boolean IsNew {get; set;}
LadderLogic
  • 1,090
  • 9
  • 17
1

In C#, if you specify what you are getting and setting, you cannot use the same name as the property (self-reference issue). As of now, you are attempting to get and set a property to itself, which is not valid. Also a heads up about naming conventions, your public properties should not begin with an underscore, but should follow capital camel casing.

There are two answers to this, both equally valid depending on what you need to do.

METHOD 1: If you take out what it is getting and setting, C# can figure out that there is an implied field that is referenced by the IsNew property. This is essentially shorthand for METHOD 2.

public bool IsNew { get; set; } // shorthand way of creating a property

METHOD 2: Specify a field to get and set.

private bool  _isNew; // the field
public bool IsNew { get => _isNew; set => _isNew = value; } // this is the property controlling access to _isNew

Read more information here: Shorthand Accessors and Mutators

Essentially, Use METHOD 1 by default if you don't need to perform any additional operations. However, if you need to provide additional functionality when getting or setting, then use METHOD 2 (I.E. look up the MVVM Pattern for an example https://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern-in-wpf/)

GLJ
  • 1,074
  • 1
  • 9
  • 17