Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access implemented property (from interface)

Tags:

syntax

c#

.net

I have an interface with properties:

public interface IEntityModifier
{

    ...
    bool AutoDetachOnFinished { get; set; }
    bool Finished { get; }
    ...

}

Then I implement it:

    bool IEntityModifier.AutoDetachOnFinished { get; set; }
    bool IEntityModifier.Finished { get { return this.mFinished; } }

But when I need to access AutoDetachOnFinished within the same class, a compiler error pops out:

    void IEntityModifier.Update(IEntity pEntity, Microsoft.Xna.Framework.GameTime pGameTime)
    {
        if (!this.mFinished)
        {
            this.Value += this.Delta * (float)pGameTime.ElapsedGameTime.TotalSeconds;

            if (this.Value >= this.Max)
            {
                this.Value = this.Max;
                this.mFinished = true;
                if (this.AutoDetachOnFinished) { /* Error Here */ }
            }
        }
    }

The error message:

14 'MEngine.Entities.EntityModifier.SingleValueEntityModifier' does not contain a definition for 'AutoDetachOnFinished' and no extension method 'AutoDetachOnFinished' accepting a first argument of type 'MEngine.Entities.EntityModifier.SingleValueEntityModifier' could be found (are you missing a using directive or an assembly reference?)

I have 2 questions:

  1. Why does the compiler complain if I delete IEntityModifier.s (so IEntityModifier.Update would become Update, apply to any implemented method)?
  2. Why can't I access AutoDetachOnFinished?
like image 885
Luke Vo Avatar asked Jan 08 '13 10:01

Luke Vo


1 Answers

You have implemented these as explicit interface implementations, meaning you can only access them through a variable of the interface type - IEntityModifier.

Either do that:

if (((IEntityModifier)this).AutoDetachOnFinished)

or remove the interface name from the implementation:

bool AutoDetachOnFinished { get; set; }
bool Finished { get { return this.mFinished; } }
like image 146
Oded Avatar answered Oct 08 '22 18:10

Oded