By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,958 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,958 IT Pros & Developers. It's quick & easy.

Overriding my method

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Overriding is basically changing the way the method/property behaves; you
cannot change the signature of a method/property when overriding. Take a
look at this document for rules on overriding:
http://msdn.microsoft.com/library/de...ingmethods.asp

To change the signature, you will have to use the Overloads keyword.

hope that helps..
Imran.

"Amin Sobati" <am***@morva.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 21 '05 #2

P: n/a
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
"Imran Koradia" <no****@microsoft.com> wrote in message
news:#n**************@TK2MSFTNGP15.phx.gbl...
Overriding is basically changing the way the method/property behaves; you
cannot change the signature of a method/property when overriding. Take a
look at this document for rules on overriding:
http://msdn.microsoft.com/library/de...us/vbcn7/html/
vaconoverridingexistingmethods.asp
To change the signature, you will have to use the Overloads keyword.

hope that helps..
Imran.

"Amin Sobati" <am***@morva.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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


Nov 21 '05 #3

P: n/a
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.

"Amin Sobati" <am***@morva.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
"Imran Koradia" <no****@microsoft.com> wrote in message
news:#n**************@TK2MSFTNGP15.phx.gbl...
Overriding is basically changing the way the method/property behaves; you cannot change the signature of a method/property when overriding. Take a
look at this document for rules on overriding:

http://msdn.microsoft.com/library/de...us/vbcn7/html/ vaconoverridingexistingmethods.asp

To change the signature, you will have to use the Overloads keyword.

hope that helps..
Imran.

"Amin Sobati" <am***@morva.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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



Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.