We help IT Professionals succeed at work.

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

Tom Knowlton
Tom Knowlton asked
on
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

Comment
Watch Question

Senior Developer
Commented:
Hard to tell without concise and complete example. E.g.

namespace ConsoleCS
{
    using System;

    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("\nDone.");
            Console.ReadLine();
        }
    }

    public interface ISalesForceRepository
    {
        string ConsumerKey { get; }
    }

    public class SalesForceRepository : ISalesForceRepository
    {
        public string ConsumerKey => "3MVG9fMtCkV6eLhf5nAFy1";
    }

    public class SalesForceRepository2 : ISalesForceRepository
    {
        string ISalesForceRepository.ConsumerKey => "3MVG9fMtCkV6eLhf5nAFy1";
    }

    public class SalesForceRepository3 : ISalesForceRepository
    {
        public string ConsumerKey { get { return "3MVG9fMtCkV6eLhf5nAFy1"} };
    }

    public class SalesForceRepository4 : ISalesForceRepository
    {
        string ISalesForceRepository.ConsumerKey { get { return "3MVG9fMtCkV6eLhf5nAFy1"} };
    }
}

Open in new window

Compiles without errors.

Commented:
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 {} };
    }
Tom KnowltonWeb developer

Author

Commented:
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?
ste5anSenior Developer
See my example in the post above. It does exactly that.
Tom KnowltonWeb developer

Author

Commented:
Thank you both!