0
class Anything { List<string> PPP = new List<string>() { "table", "chair", "spoon", "bread" }; string name; } 

name must be one of the items in the PPP.

name = "bed" //must be an error. 

How should I write property for name ? And consructor for class Anything ?

For example I can check out value every time with foreach. But I want to learn that is there another ways? PPP can be added value seldom. Therefore I dont use Enum. It is only little part of my program. And I am beginner in OOP. I dont use database or form. I work in console via oop.

5
  • 3
    "PPP can be added value seldom. Therefore I dont use Enum" - I don't understand this reasoning. This feels like a perfect fit for enums... Commented Feb 27, 2014 at 15:13
  • Seldom is not "never". I dont know english very well. may be I'm not right. Can I add item to enum seldom ? Commented Feb 27, 2014 at 15:16
  • And thanks for correction my code Commented Feb 27, 2014 at 15:16
  • Absolutely. You just change the code. Now all the code needs to be aware that the enum can be added to, but that doesn't mean it's the wrong approach - the same is true for your List<string>, of course. Commented Feb 27, 2014 at 15:16
  • Potentially, but I would probably make the enum value names themselves English, and either use attributes to include the description, or use a resource lookup for i18n. Commented Feb 27, 2014 at 15:19

3 Answers 3

1

You can control that in the set accessor for name:

class Anything { public Anything(string name) { Name = name; // this will call the `set` accessor } List<string> PPP = new List<string>() { "table", "chair", "spoon", "bread" }; private string name; public string Name { get {return name;} set { if (!PPP.Contains(value)) throw new ArgumentException("value"); name = value; } } } 

If performance is a problem change List<string> to HashSet<string>.

Sign up to request clarification or add additional context in comments.

4 Comments

He asked for a constructor. His name is a private field, not a property.
@SamLeach added the constructor - and I am using the private field as the backing field for the property.
@DStanley Yet it's the difference between an immutable and a mutable type. That's a pretty big difference.
Thank you for answers. I found my answer and also I learned another things for your codes.
0

How can I write constructor for this class?

class Anything { public Anything(string name) { if(ppp.Contains(name)) this.name = name; else throw new ArgumentException("Name invalid"); } private List<string> ppp = new List<string>() { "table", "chair", "spoon", "bread" }; private string name; } 

You may have noticed that I have applied some C# conventions to your code:

  1. Changed PPP to ppp. The convention is that private fields be lowerCamelCase.
  2. Added explicit access modifiers to your private fields. This is more personal preference.

1 Comment

Ok, I will. and Thanks for your answer. it was very helpful for me.
0
public class Anything { private List<string> PPP = new List<string>() { "table", "chair", "spoon", "bread" }; private string _name; // Public property to have access for reading it outside the class but // with private setter to set it only inside class public string Name { get { return _name; } private set { // Use linq to verify name if(PPP.Contains(value)) { _name = value; } else { string message = string.Format("{0} is not acceptable value!", value) throw new InvalidOperationException(message); } } }; // Constructor public Anything(string name) { this.Name = name; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.