-3

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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 2
    you should really learn how to show all relevant code.. you are showing us something and expect us to know what you are talking about.. we don't know what `IHHSDBUtils` looks like nor do we know what `TableExists` is supposed to be.. very poor question at best – MethodMan Dec 01 '14 at 22:18
  • 1
    Why is this question flagged as unclear? [Duplicate](http://stackoverflow.com/q/2520727/3367144), sure. But it's pretty clear what OP is asking, as indicated by the 3 correct and relevant answers. – kdbanman Nov 25 '15 at 17:54

3 Answers3

6

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.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
3

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)
    {
        // ..
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
2

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"))
abto
  • 1,583
  • 1
  • 12
  • 31