3

I have a public interface that has two methods:

public interface IClickStrategy : IStrategy
{
        void InsertSending(MailingResult sending, string connectionString, string tableName);
        void InsertClick(ClickResult click, MailingResult mailing, int campaignId, string              connectionString, string tableName);
}

And I have a class that implements this interface functions. So when I implement these functions, they are correctly in place but of course they are not public:

void IClickStrategy.InsertSending(MailingResult sending, string connectionString, string tableName){
...
}

I'm trying to call this function from my class and it gives me an error that actually makes sense:

"The name 'InsertSending' does not exist in the current context."

What I want to know is how to use these functions inside my class? What is the correct way of doing this? Thanks.

disasterkid
  • 6,948
  • 25
  • 94
  • 179

3 Answers3

10

The problem is that you're using explicit interface implementation. You don't have to do that. You could just write:

public void InsertSending(...)
{
    ...
}

and call the method normally elsewhere. If you want to use explicit interface implementation though, you can only call the method via a reference of the interface type, not your class type. So you can use:

IClickStrategy strategy = this;
strategy.InsertSending(...);

Personally I try not to use explicit interface implementation unless I have a really good reason for doing so - it tends to make things more complicated in my experience, with this just being one example of that.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I was just about to ask you why there is explicit interface implementation until you replied yourself. Correct answer indeed, sir. Thanks. – disasterkid May 31 '13 at 07:40
3

Some food for thought - click me for reference

"Explicit interface member implementations serve two primary purposes:

Because explicit interface member implementations are not accessible through class or struct instances, they allow interface implementations to be excluded from the public interface of a class or struct. This is particularly useful when a class or struct implements an internal interface that is of no interest to a consumer of that class or struct.

Explicit interface member implementations allow disambiguation of interface members with the same signature. Without explicit interface member implementations it would be impossible for a class or struct to have different implementations of interface members with the same signature and return type, as would it be impossible for a class or struct to have any implementation at all of interface members with the same signature but with different return types. "

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
1

It must be

public void InsertSending(MailingResult sending, string connectionString, string tableName){
...
}

inside a class, that is decorated as implementing this interface.

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92