Sign in to follow this  
Zyndrof

Overridable?

Recommended Posts

My VB.net-code looks like this:
'Clear - provide a new implementation of clear...
Public Overrides Sub Clear()

    'get collectionbase to clear...
    MyBase.Clear()

    'clear our hashtable...
    EmailHashtable.Clear()

End Sub

'RemoveAt - remove an item by index...
Public Overrides Sub RemoveAt(ByVal index As Integer)
    Remove(Item(index))
End Sub
I get these errors:
'Public Overrides Sub RemoveAt(index As Integer)' cannot override 'Public Sub RemoveAt(index As Integer)' because it is not declared 'Overridable'.
'Public Overrides Sub Clear()' cannot override 'Public Sub Clear()' because it is not declared 'Overridable'.
What does this mean and how do I solve it?

Share this post


Link to post
Share on other sites
For a child class to override a function from the base class, the basse class must have delcared the function as Overridable.

If you have access to the super class you can change the function definition yourself so it includes the Overridable modifier. If not, the function is not meant to be overriden and so you must find some other way of doing what you want.

Share this post


Link to post
Share on other sites
In your base class, define RemoveAt function as overridable. This way, when you want to override that function in an upper level class, such as this one, the compiler lets you do it.

Assume you have two classes, class1 and class2, and class2 inherits from class1.


Public Class Class1
Public Overridable Sub test()
MsgBox("HI, I AM AN OVERRIDABLE SUB :) ")
End Sub
End Class





Public Class Class2
Inherits Class1
'
Public Overrides Sub test()
MsgBox("I Override!")
End Sub

End Class




And on your form :



Imports TestClass

'
Public Class Form1
'
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim class1Var As New Class1
Dim class2Var As New Class2
'
class1Var.test()
class2Var.test()

End Sub



I Hope it helps in some way..

Share this post


Link to post
Share on other sites
I found the source code on Wrox's webpage (the code is from a book I'm learning from). In this code it says "Public Shadows Sub Clear()" instead of "Public Overrides Sub Clear()".

It works like it should now.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this