Click here to Skip to main content
15,886,104 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have commented out the properties and its method. How do I implement those in the interfaces and I also have commented out individual properties in each class. This is from UML diagram from Begining visual c#2012.

C#
namespace Interfaces
{
    interface IHotDrink
    {
        void Description();

       // string Instance { get; set; }
      //  /int Sugar { get; set; }
        //int AddSugar { get; set; }
        //string Milk { get; set; }
    }
  public class CupOfCoffee: IHotDrink
    { // string BeanType;
        public void Description()
      {
          Console.WriteLine("Hello, what kind of coffee would you like to have?");
      }


    class CupOfTea: IHotDrink
    { // string LeafType;
       public void Description()
        {
            Console.WriteLine("What kind of tea would you like to have?");
        }

    }
    class Program
    {
       static void Main()
        {
            CupOfCoffee C = new CupOfCoffee();
            CupOfTea T = new CupOfTea();

            C.Description();
            T.Description();
        }
    }
}}
Posted

can't implement properties in interface. define it on interface.
C#
public interface IMyInterface
{
   string Instance 
      {
          get;
          set;
      }
}

Then in the implementing class, you need to implement it

C#
class MyClass: IMyInterface{
    private string instance ;

    public string Instance {
        get {
            return this.instance;
        }
        set {
            this.instance= value;
        }
    }
}

if all the property interfaces are same for the sub classes then you can try with abstract class like below
C#
public abstract class MyAbstractClass
{
    public string Instance { get; set; }
    public int Sugar { get; set; }
    public int AddSugar { get; set; }
    public string Milk { get; set; }

    public abstract void Description();
}

public class MyClass : MyAbstractClass
{
    public override void Description()
    {
        Console.WriteLine("Hello, what kind of coffee would you like to have?");
    }
}
 
Share this answer
 
v2
Comments
DamithSL 29-Apr-14 0:38am    
@downvoter please comment if there is something wrong in my answer
You cannot implement anything inside an interface.
An interface contains only method signatures and properties that need to be implemented elsewhere.

If you want to use a partially implemented code that needs to be inherited, use an abstract class.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 29-Apr-14 0:27am    
No, interface is not a signature. You may need to rephrase it...
—SA
Abhinav S 29-Apr-14 0:40am    
Done.
Sergey Alexandrovich Kryukov 29-Apr-14 0:54am    
Abhinav, "interface contains only methods signatures" is simply incorrect. It can contain method declarations, which is not the same as signature. It can contain declaration of even instances (not event types, interface cannot contain type declarations); they are nor methods neither properties...
—SA
Abhinav S 29-Apr-14 1:34am    
I am aware of this SA. An interface can even contain events.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900