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.– Roberto de Campos
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 theC#
, in theDelphi
for example, it is exactly like this too. so I believe it may be a need for lower level.– Roberto de Campos
If you change the way it works how it can be redundant?
– Maniero
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.
– ProgMen
@Maniero I understand that
virtual
allows the envelope andoverride
sobrescreve, is only that the function of thevirtual
? Or am I completely wrong? If I’m right, I believe only theoverride
would solve, without the need of thevirtual
. So I thought it was a "redundancy", I didn’t say it. I’m pretty sure there are other reasons for the existence ofvirtual
that I don’t know about.– Roberto de Campos
Roberto I have that same doubt
– ProgMen
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 aint
.– Maniero
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.
– ProgMen