locked
Interface Implementation (Class property implementing, is internal) RRS feed

  • Question

  • I have an interface and a class that implements it. The Interface and class is this...

    public interface ICustomer
    {
        object RowVersion { get; set; }
    }
    
    public class Customer : ICustomer
    {
        internal object RowVersion { get; set; }
    }
    
    

    Basically this is the property I'm concerned about. I can't implement it unless I make it public but there's no reason that this property should be publicly accessible. Is there any other way around this?

    Wednesday, December 28, 2011 5:29 PM

Answers

  • Since you're interface is public, the members are also effectively public.  Basically, when you implement an interface, you're saying that your class fulfills the contract described by the interface.  In this case, the public interface ICustomer has a RowVersion property, so your class needs to have that as well.

     

    If you don't want this obvious when you're using the instance as a Customer (instead of as an ICustomer), there are options.

    You can make the class explicitly implement the interface, if you want it to only be exposed via that interface:

    public class Customer : ICustomer
    {
        object ICustomer.RowVersion { get; set; }
    }
    
    


    This will make it so you can only access RowVersion when using your Customer as an ICustomer...

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Pantelis44999 Wednesday, December 28, 2011 7:27 PM
    • Marked as answer by Phenomin Wednesday, December 28, 2011 10:46 PM
    Wednesday, December 28, 2011 5:36 PM

All replies

  • Friend,

      If it is private you should not declare it in Interface. Iterface mainly for declaring the public items to make sure that all those classes implementing it will have those members exposed. If you dont want to set the value in that, probably you can avoid the setter in that property.

     


    -- Thanks Ajith R Nair
    Wednesday, December 28, 2011 5:34 PM
  • Since you're interface is public, the members are also effectively public.  Basically, when you implement an interface, you're saying that your class fulfills the contract described by the interface.  In this case, the public interface ICustomer has a RowVersion property, so your class needs to have that as well.

     

    If you don't want this obvious when you're using the instance as a Customer (instead of as an ICustomer), there are options.

    You can make the class explicitly implement the interface, if you want it to only be exposed via that interface:

    public class Customer : ICustomer
    {
        object ICustomer.RowVersion { get; set; }
    }
    
    


    This will make it so you can only access RowVersion when using your Customer as an ICustomer...

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Pantelis44999 Wednesday, December 28, 2011 7:27 PM
    • Marked as answer by Phenomin Wednesday, December 28, 2011 10:46 PM
    Wednesday, December 28, 2011 5:36 PM
  • Hmmm. Strangely, this works perfectly in VB using the Friend keyword in place of internal. I'll try your suggestion and see if it will work with the rest of my code and let ya know. Thanks.

    Wednesday, December 28, 2011 5:46 PM
  • How can I set this property from within a method in the Customer class?
    Wednesday, December 28, 2011 6:05 PM
  • How can I set this property from within a method in the Customer class?

    You can either have a separate (private) property, or you can cast:

    (this as ICustomer).RowVersion = theNewValue;
    



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, December 28, 2011 6:33 PM
  • Hello,

    What is the reason for implementing this interface ? it doesn't really make sense.

    An interface is used whenever you want to define a contract with very specific functionality between unrelated classes, meaning, that the consumer doesn't really care what object will do the job as long as it's done by something that implements the contract.

    Say that we have the following classes Customer, SpecialCustomer and BadCustomer why would I implement this interface when all of them are actually a kind of Customer ?


    Eyal (http://shilony.net), Regards.

    • Edited by Eyal Solnik Wednesday, December 28, 2011 6:56 PM
    Wednesday, December 28, 2011 6:47 PM
  • I'll try your suggestion later tonight. The reason I'm using an interface is because I'm passing the Interface to a SQLBuilder class that extracts the values of the Interface and turns them into SQL parameters. I have to use the Interface otherwise it will create a circular dependency with referenced projects.

    Wednesday, December 28, 2011 9:40 PM