Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Why Use Interfaces
Message
From
28/12/2007 16:31:37
 
 
To
28/12/2007 16:16:30
General information
Forum:
ASP.NET
Category:
Class design
Miscellaneous
Thread ID:
01278205
Message ID:
01278213
Views:
28
Good point Bill!!! (I should probably add that to my "stock answer".)

But, you'd probably also need to cast to the Interface:
If (MyControl is MyControlInterface1)
{
    ((MyControlInterface1)MyControl).MyMethod();
    ((MyControlInterface1)MyControl).MyProp = ...
}
BTW, it's pretty standard to preface all interfaces with an "I", but you probably already knew that.

~~Bonnie



>Hi Kevin,
>
>In addition to Bonnie's excellent info, we have to create questionnaires that require iteration of various controls on forms and execution certain methods and/or access certain properties that some but not all controls may have. With controls developed to an interface it's really to use contructs like:
>If (MyControl is MyControlInterface1)
>{
>    MyControl.MyMethod();
>    MyControl.MyProp = ...
>}
>
>Bill
>
>>I understand that an interface forces a class to use a defined set of properties and methods.
>>
>>In the code below, why use the interface? This seems to me to limit the programmer
>>who inherits from this class by forcing him/her to add code to their class that they
>>may not use. Why not just allow the programmer to implement the class any way they want.
>>
>>Can someone provide a more concrete purpose for an Interface? I'm guessing that I don't
>>full understand.
>>
>>Thanks
>>
>>
>>
>>using System;
>>using System.Collections.Generic;
>>using System.Text;
>>
>>namespace Interface1
>>{
>>    interface IMessage
>>    {
>>        // Send the message. Returns True if success, False otherwise
>>        bool Send();
>>
>>        // The message to send
>>        string Message { get; set; }
>>
>>        // The address to send to
>>        string Address { get; set; }
>>
>>    }
>>
>>    public class EmailMessage : IMessage
>>    {
>>        string _address = "";
>>        string _message = "";
>>
>>        public bool Send()
>>        {
>>            bool bRetVal = false;
>>            return bRetVal;
>>        }
>>
>>        public string Message
>>        {
>>            get
>>            {
>>                return _message;
>>            }
>>            set
>>            {
>>                _message = value;
>>            }
>>        }
>>
>>        public string Address
>>        {
>>            get
>>            {
>>                return _address;
>>            }
>>            set
>>            {
>>                _address = value;
>>            }
>>        }
>>
>>    }
>>}
>>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform