samedi 1 janvier 2022

Decorator pattern on a winform panel

I am trying to make a decorator pattern with an IPanel interface but when I try to access the base class I get NullReferenceException. I would use composite but I am forced to use decorator on this one.

Thats the decoration I implemented:

public interface IPanel
    {
        void Operation();
    }

    public class CorePanel : Panel, IPanel
    {
        public void Operation() { }
    }
    public class PanelDecorator : IPanel
    {
        protected IPanel m_Decorated;
        public PanelDecorator(IPanel i_Decorated)
        { m_Decorated = i_Decorated; }

        public virtual void Operation()
        { m_Decorated.Operation(); }

    }
    public class VScrollPanel : PanelDecorator
    {
        public VScrollPanel(IPanel i_Decorated) :
        base(i_Decorated){ }
        public override void Operation()
        {
            Scroll();
        }
        private void Scroll()
        {
            (this.m_Decorated as Panel).Visible = false;
        }
    }
       private void InitializeComponent()
    {
        // 
        // panel1
        // 
        this.panel1.Location = new System.Drawing.Point(29, 81);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(409, 341);
        this.panel1.TabIndex = 5;
        // 
        // DecoratedPanel
        // 
        this.ZodiacIPanel = new VScrollPanel(new CorePanel());
        (this.ZodiacIPanel as CorePanel).Visible = false;{this one gives the null ptr error}

#endregion
    private IPanel ZodiacIPanel;
    private System.Windows.Forms.Panel panel1;

Aucun commentaire:

Enregistrer un commentaire