Search code examples
c#dispose

'Dispose' ambiguity error in C#


I have the following code:

public partial class Painter : Form
    {
        private System.ComponentModel.Container Components = null;

        private const int m_intDIAMETER = 8;

        private const int m_intMOUSEUP_DIAMETER = 4;

        private Graphics m_objGraphic;

        private bool m_binShouldPaint = false;

        private bool m_binShouldErase = false;



        public Painter()
        {
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                    //components = null;
                }
            }
            base.Dispose(disposing);
        }
        static void Main()
        {
            Application.Run(new Painter());
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            m_objGraphic = CreateGraphics();

        }

        private void Painter_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e)
        {
            //m_objGraphic.FillEllipse(new SolidBrush(Color.HotPink), e.X, e.Y, m_intDIAMETER, m_intDIAMETER);
            //m_binShouldPaint = true;

            if (e.Button == MouseButtons.Left)
            {
                m_binShouldPaint = true;
            }
            else if (e.Button == MouseButtons.Right)
            {
                m_binShouldErase = true;
            }
        }

At the compile time my debugger generates the following error:

Error   1   Type 'Painter.Painter' already defines a member called 'Dispose' with the same parameter types

I think the Dispose method is generated by the program and that's why when I write it without generating the "Dispose" it gives me an error. But how can I fix it?


Solution

  • You have another Dispose method in your designer generated file. i.e. Painter.Designer.cs, if you have some custom implementation in Dispose, then modify the one in Painter.Designer.cs file or move it to your code behind.

    Visual studio generates a designer.cs file with every Form, that file contains code regarding controls used on the form and also has Dispose method implementation. Since your code behind has one, you are getting an error of having multiple Dispose method in a same class. (Both files have the same class through partial keyword)

    enter image description here