What is the need of Virtual to overwrite a method in another class?

Asked

Viewed 177 times

0

I am developing a C# system and I came across an inheritance/polymorphism problem. I searched in Google and in Microsoft references, however, I did not find something to clarify my doubts. I am using an event click in an object NavigatorToolbar to enable a new registration, however, in this click, I wanted you to click and get some information from this base class of mine.

public partial class formToolbar : formDefault
{
    public formToolbar()
    {
        InitializeComponent();
        novo.Enabled = true;
    }
    private void novo_Click(object sender, EventArgs e)
    {
        novoRegistro();
    }
    public virtual void novoRegistro() 
    {

    }
    public void novoRegistro(string t)
    {
        MessageBox.Show(t);
    }
}

This code is where you are receiving the event click, and within the function novoRegistro, will be where I will implement the creation of a new record.

public partial class inssCadastroGui : formToolbar
{
        public inssCadastroGui()
        {
            InitializeComponent();
        }

        public override void novoRegistro()
        {
            string teste = "eu sou um parametro";
            base.novoRegistro(teste);
        }
}

And that code is where it’s passing the value to the function. However, when I take the virtual of the code, it stops working. I would like to do so because it would build the bank/system interaction through procedures, thus, through this click, on each screen I would pass the data dynamically. What is the need of this virtual for the creation of this interaction ?

  • It is mandatory because it says that the method can be overwritten. If you do not have the virtual, means that this method cannot be overwritten.

  • But just today I was thinking about it, virtual seems to be a redundancy. I do not know if it is a matter of security, organization or anything else. But this is not so only in the C#, in the Delphi for example, it is exactly like this too. so I believe it may be a need for lower level.

  • If you change the way it works how it can be redundant?

  • I didn’t understand why it was marked as duplicate, I couldn’t find a question like that. However, the signature for the method to be rewritten is not just the override notation ? NOTE: I’m starting with c#, it’s been about 3 months since I started.

  • @Maniero I understand that virtual allows the envelope and override sobrescreve, is only that the function of the virtual? Or am I completely wrong? If I’m right, I believe only the override would solve, without the need of the virtual. So I thought it was a "redundancy", I didn’t say it. I’m pretty sure there are other reasons for the existence of virtual that I don’t know about.

  • Roberto I have that same doubt

  • Let me repeat: if the virtual works one way and with it works another, where is the redundancy? The reason is you want the method to be virtual if you don’t want to use it. If you don’t have to use it override, if using in non-virtualized method is error, it is the same thing as trying to add a text to a int.

  • Actually, in that case when I delete the new method Empty log, override does not find any methods with the same characteristics to overwrite. Thank you.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.