Why does this implementer method not see its sibling?

103 views Asked by At

I've got a class that implements an interface:

public class SQLiteHHSDBUtils : IHHSDBUtils
{

    void IHHSDBUtils.SetupDB()
    {
            . . .
            if (!TableExists("AppSettings"))

    . . .

    bool IHHSDBUtils.TableExists(string tableName)
    {
    . . .

It can't find its own brother sitting right below it (the if (!TableExists()):

The name 'TableExists' does not exist in the current context

How can it / why does it not see it?

3

There are 3 answers

0
Selman Genç On BEST ANSWER

You have an explicit interface implementation. You can't access your interface methods directly unless you cast current instance to interface type:

if (!((IHHSDBUtils)this).TableExists("AppSettings"))

From 13.4.1 Explicit interface member implementations

It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.

0
abto On

TableExists is an explicit implementation. If you want to access it, you have to cast this to IHHSDBUtils:

void IHHSDBUtils.SetupDB()
{
    . . . 
    if (!((IHHSDBUtils)this).TableExists("AppSettings"))
0
Blorgbeard On

When you explicitly implement an interface, you need to access the interface member from a variable whose type is exactly the interface (not an implementing type).

if (!TableExists("AppSettings")) is calling TableExists via the this object, whose type is SQLiteHHSDBUtils, not IHHSDBUtils.

Try:

if (!((IHHSDBUtils)this).TableExists("AppSettings"))

Alternatively, don't explicitly implement the interface:

public class SQLiteHHSDBUtils : IHHSDBUtils
{
    // .. 
    bool TableExists(string tableName)
    {
        // ..