Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Why Use Interfaces
Message
De
02/01/2008 12:40:23
 
 
À
28/12/2007 16:31:37
Information générale
Forum:
ASP.NET
Catégorie:
Conception classe
Divers
Thread ID:
01278205
Message ID:
01279012
Vues:
28
Hey Bonnie,

Yes on both points. My interfaces all begin with "I" as you note and I was just giving an example for clarification. And yes, the cast has to be made for real. I was at home and in a hurry when I made the post so I neglected that little detail. Good call.

Bill

>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;
>>>            }
>>>        }
>>>
>>>    }
>>>}
>>>
William A. Caton III
Software Engineer
MAXIMUS
Atlanta, Ga.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform