views:

214

answers:

2

i want a button that when it's pushed a new string is showen in the textboxes

what am i doing wrong

can somone inlight me why doesnt this code works ? ...

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public event Startdelegate StartEvent;
        myButton button;
        newTb[] tb;
        public Form1()
        {
            InitializeComponent();

            button = new myButton();
            button.Parent = this;
            button.Location = new Point(120, 0);
            button.Text = "click on me!!!";
            tb = new newTb[8];

            for (int i = 0; i <= 80; i += 15)
            {
                tb[i / 15] = new newTb();
                tb[i / 15].Parent = this;
                tb[i / 15].Location = new Point(i + i, i + i);
               // button.Subscribe(tb[i / 15]);
            }

            button.Click += new EventHandler(button_Click);

        }
        private void button_Click(object sender, EventArgs e)
        {
            button.s = "this is clicking";
            //button.Notify();
        }
    }

    public class myButton : Button, IObservable<newTb>
    {
        public string s;
        private List<IObserver<newTb>> observers;

        public myButton()
        {
            observers = new List<IObserver<newTb>>();
        }

        public IDisposable Subscribe(IObserver<newTb> observer)
        {
            if (!observers.Contains(observer))
            {
                observers.Add(observer);           
            }
            return new Unsubscriber(observers, observer);  
        }

        protected void Notify(newTb tb)
        {
            foreach (IObserver<newTb> observer in observers)
            {
                observer.OnNext(tb);
            }
        }

        #region Unsubscriber
        private class Unsubscriber : IDisposable
        {
            private List<IObserver<newTb>> observers;
            private IObserver<newTb> observer;

            public Unsubscriber(List<IObserver<newTb>> observers, IObserver<newTb> observer)
            {
                this.observers = observers;
                this.observer = observer;
            }

            public void Dispose()
            {
                if (observer != null && observers.Contains(observer))
                {
                    observers.Remove(observer);
                }
            }
        }
        #endregion 

        class newTb : TextBox, IObserver<string>
        {
            string s;
            public void OnCompleted() { }
            public void OnError(Exception error) { }
            public void OnNext(string value)
            {
                this.Text = value;
            }
        }
    }
}
+5  A: 
Frederic Torres
thank u it works can you in-light with some facts I don't understand where i got wrong
yoav.str
Actually I also have a problem with the IObserver/IObservable pattern. I will try to write your problem from scratch tonight.
Frederic Torres
thank you very much .
yoav.str
I changed may answer with this time something that make sense
Frederic Torres
+1  A: 

I believe this could be your problem:

class newTb : TextBox, IObserver<string>

Because what you what you wanted to observe based on this sample:

observers = new List<IObserver<newTb>>();

is actually IObserver<newTb>, which is different type than IObserver<string>. Your newTb class does not implement first interface, it only implements the latter. Not sure why this compiles (if it does), though.

Paweł Dyda