468,785 Members | 1,730 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

Overriding my method

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
3 1351
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
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
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.

Similar topics

3 posts views Thread by Andrew Durdin | last post: by
9 posts views Thread by James Marshall | last post: by
17 posts views Thread by Bob Weiner | last post: by
6 posts views Thread by John Cobb | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.