Overridable?

Started by
3 comments, last by Zyndrof 17 years, 11 months ago
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?
Advertisement
Public Overridable Sub Clear ()        'get collectionbase to clear...    MyBase.Clear()    'clear our hashtable...    EmailHashtable.Clear()End Sub

??

Beginner in Game Development?  Read here. And read here.

 

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.
"We confess our little faults to persuade people that we have no large ones." -Francois de La Rochefoucauld (1613 - 1680). | My blog
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 SubEnd Class


Public Class Class2    Inherits Class1    '    Public Overrides Sub test()        MsgBox("I Override!")    End SubEnd 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..
Have we sent the "Don't shoot, we're pathetic" transmission yet?
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.

This topic is closed to new replies.

Advertisement