The method or operation is not implemented
Asked Answered
R

3

14

There are two forms. Form2 is derived from Form1.

But I have an issue with Form2 in design mode as shown on the screenshot below.

If I will comment this this._presenter.Retrive(); it will work fine. Whats going on and how to solve the problem?

UPD: If I will remove the throw new NotImplementedException(); and will insert, for example, MessageBox.Show("Test");, every time I will open Form2 the MessageBox will appears as though I run the application.

enter image description here

Form2

namespace InheritanceDemo
{
    public partial class Form2 : Form1
    {
        public Form2()
        {
            InitializeComponent();
        }
    }
}

Form1

namespace InheritanceDemo
{
    public partial class Form1 : Form
    {
        protected IPresenter _presenter;

        public Form1()
        {
            InitializeComponent();
            _presenter = new Presenters();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this._presenter.Retrive();
        }
    }

    public class Presenters : IPresenter
    {
        public void Retrive()
        {
            throw new NotImplementedException();
        }
    }

    public interface IPresenter
    {
        void Retrive();
    }
}
Rime answered 2/1, 2016 at 15:42 Comment(5)
Instead of throw new NotImplementedException(); do something else.... :)Nsf
@FᴀʀʜᴀɴAɴᴀᴍ If I will remove the throw new NotImplementedException(); and will insert, for example, MessageBox.Show("Test");, every time I will open Form2 the MessageBox will appears as though I run the application.Rime
By something else I meant some logic your interface is supposed to implement. Not just anything like MessageBox.Nsf
@FᴀʀʜᴀɴAɴᴀᴍ I understood. But why is there such a behavior?Rime
you will find Can't view designer when coding a form in C# and Show controls added programatically in WinForms app in Design view posts helpful and interesting!Zymogenic
Z
20

The main cause of error is what Fᴀʀʜᴀɴ and Yuval said:

throw new NotImplementedException();

But there is another important thing that you should pay attention to.

OP: If I will remove the throw new NotImplementedException(); and will insert, for example, MessageBox.Show("Test");, every time I will open Form2 the MessageBox will appears as though I run the application

If you notice, you will not receive this error in designer of Form1. But because your Form2 inherits from Form1 you receive this error.

It's because, when you open a form in designer, the designer makes an instance of base class of your form to show your form. It means instead of creating an instance of Form2 it creates an instance of Form1, runs Form1 constructor and hosts it in the design surface, and then deserializes the codes in InitializeComponent of Form2 and puts components on the design surface.

This is why you receive the error when you see your Form2 in designer, but you didn't receive any error while opening the Form1 in designer.

To solve the problem:

  • You can remove the implementation, and let the implementation be empty.
  • Also you can prevent the error by prevent running the code in Form_Load fd you are at design mode using DesignMode property, in Form1_Load:

    if (DesignMode) return;

You probably will find these answers helpful and interesting:

Zymogenic answered 2/1, 2016 at 16:2 Comment(9)
@FᴀʀʜᴀɴAɴᴀᴍ Sorry about misspelled name :)Zymogenic
But what if my DoSometning() method is not working correctly right now.Rime
What do you expect as result?Zymogenic
I want to finish the application framework, then go and do the design, and then write the implementation.Rime
You can remove the implementation, and let the implementation be empty. Also you can prevent the error by prevent running the code in Form_Load id you are at design mode.Zymogenic
What do you mean? How can I prevent running the code at design mode?Rime
What exactly is DesignMode in your example?Rime
DesignMode Gets a value indicating whether a control is being used on a design surface.Zymogenic
@MikhailDanshin It seems the post answers your question and it would be great if you accept the post :)Zymogenic
J
5

Whats going on and how to solve the problem?

This is fairly trivial. If you would of debugged your code, you'd see that you're throwing a NotImplementedException in your method call, that is why commenting it out works:

public void Retrive()
{
    throw new NotImplementedException();
}

Instead of throwing, perhaps you want to implement the actual method logic.

Jeremiah answered 2/1, 2016 at 15:54 Comment(0)
A
1

Commenting out the part throw new NotImplementedException(); worked fine for me Now, finally method will be alike below:

public void Retrive()
{
    //throw new NotImplementedException();
}
Alkene answered 9/11, 2016 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.