disasterkid
disasterkid

Reputation: 7278

Cannot Call Implemented Functions of Interface (The Name Does Not Exist In the Current Context)

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.

Upvotes: 3

Views: 1788

Answers (3)

BLoB
BLoB

Reputation: 9725

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. "

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500245

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.

Upvotes: 10

Eugen Rieck
Eugen Rieck

Reputation: 65274

It must be

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

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

Upvotes: 1

Related Questions