Interfaces and Properties

1870 pts.
Tags:
C#
Class
interface
Public 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#

Answer Wiki

Thanks. We'll let you know when a new response is added.

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

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Gent01
    Yes, I have used casting to do what your last sentence said.
    1,870 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following