Why the property of the class implementing the interface not working?

Monah

Kindly, i need to know what i am missing or what i am doing wrong with the following code?

public class Program
{
    public static void Main()
    {
        var a = new A();
        a.Controls.Add(new B());
        a.Controls.Add(new C());
        a.Controls.Add(new D());
        a.DisplayMode = DisplayMode.Edit;
    }
}
public enum DisplayMode
{
    View = 0,
    Edit = 1
}

public interface IMode
{
    DisplayMode DisplayMode { get; set; }
}

public class A
{
    private DisplayMode mDisplayMode;
    public A()
    {
        mDisplayMode = DisplayMode.View;
        Controls = new List<object>();
    }
    public DisplayMode DisplayMode
    {
        get
        {
            return mDisplayMode;
        }
        set
        {
            if (mDisplayMode != value)
            {
                mDisplayMode = value;
                foreach (var control in Controls)
                    if (control is IMode)
                        (control as IMode).DisplayMode = value;
                // the control ( b ).DisplayMode is not firing or executing the set
                // what i am missing here?
            }
        }
    }
    public List<object> Controls { get; set; }
}

public class B : IMode
{
    private DisplayMode mDisplayMode;
    public B()
    {
        X=0;
        mDisplayMode=DisplayMode.View;
    }

    public int X { get ; set;}

    public DisplayMode DisplayMode
    {
        get
        {
            return mDisplayMode;
        }
        set
        {
            if (mDisplayMode != value)
            {
                mDisplayMode = value;
                // some code should be executed;
                X=10;
            }
        }
    }
}

public class C {}

public class D {}

after i call a.DisplayMode=DisplayMode.Edit i am expecting that all controls ( B ) will execute the set of DisplayMode, but after executing the code, its not firing at all

what i mean by the DisplayMode setter value is not executing because if so then X value should become 10

Monah

as they are saying, your code should work without any problem

can you post your original code, maybe you are missing something that changing the behavior of your code ?

if you insist that the setter is not called then try to call it using reflection, for example

instead of (control as IMode).DisplayMode, you can do the following

var type=control.GetType();
var property=type.GetProperty("DisplayMode");
property.SetValue(control,value,null);

hope this will help you

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why Abstract Class Implementing Interface?

Interface Implementing with interface property

Why is the List Class Implementing IList Interface?

Implementing an interface with a property that is derived

Base class implementing interface

Why is a generic implementing an interface not covariant, but a generic of a base class is?

Why type constraint in a generic class is not enough for the compiler when implementing an interface?

Why can't a generic member function in a class implementing an interface take an argument of the type of the class (instead of the interface)?

Implementing a copy method of a class implementing an interface - Java

Class design:class implementing an Interface implementing another interface

AutoConfiguredMoqCustomization with abstract class implementing interface

VBA implementing an interface with a custom class

Setting setter of an interface in class implementing it

Typescript: declare class implementing interface

Struct vs class implementing an interface

Error implementing Interface Methods in Class

JML specification in the interface and the implementing class

Nested Interface and Implementing class behavior

Implementing an Interface on a class with Dependency Injection

How can an interface expose a public property/method that does not exist in the implementing class?

Interface with generic type of the class implementing the interface?

PhpDoc for interface and class implementing interface - difference

Why can i change an implementation of property defined in interface in the class

difference of property in interface and class

Moq an interface property of a class

Why is the following code working with an interface but without any class defined?

An abstract class in Java need not implement any methods from its implementing interface. Why?

Why is my generic class constructor refusing to take objects implementing the requested Interface

Why are C# 4 optional parameters defined on interface not enforced on implementing class?