1,855 pts.
 Interfaces and Properties
Since you can't put properties into a c# interface, how do you expose each classes public properties when consuming the class as an interace instance?


interface iAnimal

{

    bool Load(int Id);

    bool Save();

}



class Fish : iAnimal

{
    private string description;


    public string Description

    {
         get { return this.description; }
         set { this.description = value; }

    }

    public bool Load(int Id)
    {
        ...
    }



    public bool Save()

    {

        ...

    }

}







class Dog : iAnimal


{
    private string description;

    private string hypernessLevel;




    public string Description


    {

         get { return this.description; }

         set { this.description = value; }


    }



    public string hypernessLevel



    {


         get { return this.hypernessLevel; }


         set { this.hypernessLevel = value; }



    }



    public bool Load(int Id)

    {

        ...

    }






    public bool Save()


    {


        ...


    }


}


iAnimal animal;

Switch (TypeOfAnimal)
{
    case "Fish":
        animal = new Fish();
        break;
    case "Dog":

        animal = new Dog();

        break;
}

txtDescription.Text = animal.Description //How would you expose description and dog's hpernessLeve?


Software/Hardware used:
VS 2005, C#
ASKED: February 23, 2010  5:55 PM
UPDATED: February 24, 2010  10:18 PM

Answer Wiki:
Actually, you can define properties in a C# interface. This is allowed: <pre>interface iAnimal { string Description { get; set; } bool Load(int Id); bool Save(); } class Fish : iAnimal { private string description; <b>public string Description { get { return this.description; } set { this.description = value; } } </b> public bool Load(int Id) { ... } public bool Save() { ... } }</pre> On the other hand, if you want to access members of a derived class that are not defined in the interface <i><b>I think </b></i>you would have to use a variable of the type of the class to instantiate it (i.e. Dog myDog = new Dog()) (I might be wrong on this). -CarlosDL
Last Wiki Answer Submitted:  February 23, 2010  8:19 pm  by  carlosdl   63,535 pts.
All Answer Wiki Contributors:  carlosdl   63,535 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Yes, I have used casting to do what your last sentence said.

 1,855 pts.