Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

Interface - how to implement read-only values. C# 7.3

How do I declare a read only value in an interface and properly declare it in a class that uses the Interface?

 public interface ISalesForceRepository
    {
        string SecurityToken { get; }
        string ConsumerKey { get; }
        string ConsumerSecret { get; }
        string Username { get;  }
        string Password { get; }

     ...

...

    }

Open in new window



public class SalesForceRepository : ISalesForceRepository
{



Says "Error	CS0535	'SalesForceRepository' does not implement interface member 'ISalesForceRepository.ConsumerKey.set'	SalesForceRepository":

string ISalesForceRepository.ConsumerKey
{
            get
            {
                return "3MVG9fMtCkV6eLhf5nAFy1[truncated].";
            }            
}

}

Open in new window



Says "The name 'Consumer Key' does not exist (same for the other params)":
await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password, url);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you're seeing this:
Error      CS0535      'SalesForceRepository' does not implement interface member 'ISalesForceRepository.ConsumerKey.set'      SalesForceRepository"

...then it's basically saying that your interface definition requires a "set" method in the implementation. So if you're not the one in control of the interface definition, then you can just choose to not do anything in set of your implementation (stealing from ste5fan's code above):


    public class SalesForceRepository3 : ISalesForceRepository
    {
        public string ConsumerKey { get { return "3MVG9fMtCkV6eLhf5nAFy1"} set {} };
    }
Avatar of Tom Knowlton

ASKER

I do control the interface as well as the implementation so I can make whatever changes are needed.

I just want to ensure that the interface declares these as read only private properties.  Possible?
See my example in the post above. It does exactly that.
Thank you both!