-1

I have a class below, i have extracted all the properties to a Interface but i don't seem to be able to extract it... Obviously create a new object like

ITestItem item = new TestItem(); 

doesn't give me access to Properties which is an instance of Meta class.

I also wanted to stop anyone from create an instance of Meta class outside of TestItem... i tried marking it as internal but that would allow me because Properties is public.

Also i am unsure whether i need to have an interface for META??

here is my class... can anyone help?

public class TestItem : ITestItem { public bool Enabled { get; set; } public Meta Properties = new Meta(); public List<int> Items { get; set; } public class Meta { internal Meta { } public string Name { get; set; } } public TestItem() { this.Items = new List<int>(); } } 

EDIT I have uppdated the class above to include an internal constructor for Meta so it can't be instanciated outside the class.

Here is my interface i have (as suggested by giddy)... It says now that it doesn't implement Properties

public interface ITestItem { bool Enabled { get; set; } Meta Properties { get; set; }; List<int> Items { get; set; } } 
1
  • your asking for a property in your interface but implementing a field instead. I Updated my answer. Commented Mar 24, 2011 at 11:32

2 Answers 2

2

So you would:

  1. Not want to maybe use the term extract to interface, maybe your idea about interfaces is a little wrong. You want to do some reading here.

  2. Define the Meta class inside the Test class. Mark the constructor internal or private depending on where you want to create an instance.

  3. Make a property that exposes the Meta class outside of the Test class

    public class TestItem : ITestItem { public TestItem() { this.Properties = new Meta();//set it from here this.Items = new List<int>(); } public bool Enabled { get; set; } //make it private set if you don't want an outsider setting it public Meta Properties {get;private set} public List<int> Items { get; set; } public class Meta {//make it private if you only create an instance here. internal Meta(){} public string Name { get; set; } } } 

You also add the Meta property to your interface:

public interface ITestItem { bool Enabled { get;set;} Meta Properties { get;set;} List<int> Items { get;set;} void ScheduleItem(); } 
Sign up to request clarification or add additional context in comments.

10 Comments

Using Meta and Meta. 1 as a class and the other as the property name is good practice?????
Uh wouldn't that break compilation as you can't expose a private class as a public property? @Martin you should add an internal constructor to your Meta class to prevent external classes from constructing it.
Marking the Meta as Private is the same as internal ..., gives the following error Inconsistent accessibility:
excellent tom! internal constructor... works a treat... but still confused about how i would use the Interface.. when i have another class inside my class
@Martin yes sorry, my mistake, just tried it, thanks @Tom yea internal makes sense. Updated answer. Also a property name the same as your class I think, it perfectly fine if it is semantically correct. .NET is full of examples. Form.Size returns a type Size (There are many more)
|
1

You may try this one. Its compiled in VS2010. It is better anyway to extract an interface for Meta also for the sake of "decoupling classes" to allow unit testing. Please search and read about - "decoupling classes".

public class Meta { // Do not make this class a child class for flexibility and testing purposes. public string Name { get; set; } } public class IMeta { string Name { get; set; } } public class TestItem : ITestItem { public TestItem() { this.Meta = new Meta(); this.Items = new List<int>(); public bool Enabled { get; set; } public IMeta Meta { get; internal set; } public List<int> Items { get; set; } } public interface ITestItem { bool Enabled { get; set; } IMeta Meta { get;} IList<int> Items { get; set; } } 

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.