0

I'm trying to add some objects in a listbox (throw a binding list), but when i'm displaying the list, the static member of the class has always the same display, the last value. There is no problem in my list or class for the other properties.

In my form:

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
         shape.gsPtAcc = ptdep;

         if (shape is Segment)
         {
             seg = shape as Segment;
             seg.gsScPtAcc = ptarr;
         }

         shape.incremNumber();
         Console.WriteLine(shape.Number);//write the correct value
         shapeList.Add(shape);
         listBox1.DataSource = null;
         listBox1.DataSource = shapeList;
        }
    }

In my mother class, shape:

namespace MyGraphicComponents
{
    abstract class Shape: IComparable<Shape>, IDrawable, INotifyPropertyChanged, IIsPointIn
    {
        protected MyPoint ptAcc;
        public MyPoint gsPtAcc
        {
            get { return ptAcc; }
            set { ptAcc = value; }
        }

        protected static int compt = 0;

        public int Number
        {
            get { return compt; }
            set { compt = value; }
        }
        public int incremNumber ()
        {
            return ++compt;
        }
    }
}

So the result, when i'm adding shape will be:

  • In the console: 1 2 3 4 5

  • But in the listbox: 5 5 5 5 5

1 Answer 1

2

A static member of a class will have the same value in all instances of this class classes

read more here : https://msdn.microsoft.com/fr-fr/library/98f28cdx.aspx

replace

  protected static int compt = 0; 

with

protected int compt = 0;
Sign up to request clarification or add additional context in comments.

3 Comments

Ok thanks, I solved it by Using number as a property, and by this way, I have been able to keep the static propertie, just have to do Number=incremNumber ();
@RemiHirtz why do you need this static property its seems like a mise use of static
It's for school, I don't have the choice :s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.