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