Overriding my method

  • Thread starter Thread starter Amin Sobati
  • Start date Start date
A

Amin Sobati

Hi,
I have two classes. Class2 inhertis Class1:
-----------------------------
Public Class Class1
Public Overridable Sub MySub()

End Sub
End Class

Public Class Class2
Inherits Class1
Public Overrides Sub MySub()

End Sub
End Class
-----------------------------
When I write MySub with another signature in Class1, MySub in Class2 becomes
underlined and indicates an error:
-----------------------------
Public Class Class1
Public Overridable Sub MySub()

End Sub
Public Overridable Sub MySub(ByVal i As Int16)

End Sub
End Class

Public Class Class2
Inherits Class1
Public Overrides Sub MySub()

End Sub
End Class
-----------------------------
I know that I can fix it by adding Overloads keyword to MySub in Class2, but
I want to know the reason that why it's not possible to override it without
overloading?
Any help would be greatly appreciated.
Amin
 
Thanks Imran,
But I think I don't manipulate it's signature. If my overriding is
considered as signature change, then why it is not for the first
situation(when there are one MySub in Class1, instead of two overloaded)? In
the first code the I pasted here, I can override without any problem.
Problem occurs as I add another MySub(with different signature) to Class1.
Amin
 
Sorry - my mistake. I misunderstood the problem. When you add another MySub
with different signature to Class1, you'll have to use the overloads keyword
there. So your two classes should look something like:

Public Class Class1
Public Overridable Overloads Sub MySub()

End Sub
Public Overridable Overloads Sub MySub(ByVal i As Int16)

End Sub
End Class

Public Class Class2
Inherits Class1
Public Overrides Sub MySub()

End Sub
End Class

I think you should be fine then. However, if you are going to override the
MySub with the new signature as well, then I think you don't need the
Overrides keyword anymore. Only the Overloaded is needed. Something like:
Public Class Class1
Public Overridable Overloads Sub MySub()

End Sub
Public Overridable Overloads Sub MySub(ByVal i As Int16)

End Sub
End Class

Public Class Class2
Inherits Class1
Public Overloads Sub MySub()

End Sub

Public Overloads Sub MySub(ByVal i As Int16)

End Sub
End Class

However, I'm not absolutely sure of that but I do remember reading that
somewhere. You can try with the Overrides or without it and see which one
works. But yes - the first piece of code should resolve what you are looking
for.

hope that helps..
Imran.
 
Imran,
The first code you wrote has the same problem. Overloads keyword does not
solve the problem and the code did not compile. You must include Overloads
keyword in class2.

Amin
 
Back
Top