Results 1 to 9 of 9

Thread: Overriding Function in VB.NET - plz help!

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Posts
    8

    Overriding Function in VB.NET - plz help!

    i tried overriding the clear() subroutine for system.collection.collectionbase class

    it gives me an error message :-

    An unhandled exception of type 'System.TypeLoadException' occurred in Structure Demo.exe

    Additional information: Declaration referenced in a method implementation can not be a final method. Type: Structure_Demo.CustomerCollection. Assembly: Clear.




    i tried overriding other functions for other classes and it work. but i dont understand why it doesnt work for the CollectionBase class
    does that mean there are problems with this specific class?
    can anyone plz help??

    Jeffrey

  2. #2
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191
    try calling the MyBase.Clear at the end of your override there may be some nessesary code that ms uses in that method the documentation says you should always pass it back to the correct event/method after override
    Magiaus
    Visual Basic 6.0 SP5
    Visual C++ 6.0 SP5


    The only sovereign you can allow to rule you is reason.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Posts
    8
    it says the sub clear() is not overridable in the definition for the collectionbase class

    but if i dont declare the Clear() as overrides on my inherited class, it underlines the Clear() in blue. it says that :-

    sub 'Clear' shadows an overridable method from the base class. in this case it should be the collectionBase class.



    im confused here...

    anyone help?

    Jeffrey

  4. #4
    New Member
    Join Date
    Mar 2002
    Posts
    3
    If you do not put the Overrides keywork in the derived class, the function is assumed to shadow the origional function.

    A shadow function is a function in the derived class that has the same name as a function in the base class but is NOT intended to override the base class function.

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    If a method is not overidable, then you cant override it. You can either use a different name for your clear method, or change the method signiture in you class.

    Hope that helps
    Dont gain the world and lose your soul

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Posts
    8
    thx for the replies...

    i'ved solved the problem by using the Shadows keyword in the derived class.
    Visual Basic 6.0
    Visual Basic .NET
    C/C++
    JAVA

  7. #7
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    please elaberate

    post the override with the shadows i am curius to see it
    Magiaus

    If I helped give me some points.

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Posts
    8
    Public Class CustomerCollection
    Inherits System.Collections.CollectionBase


    'members...
    Private _emailHashtable As New Hashtable()

    'EmailHashtable - return the e-mail hashtable...
    Public ReadOnly Property EmailHashtable() As Hashtable
    Get
    Return _emailHashtable
    End Get
    End Property


    'Item - return a customer given an index...
    Default Public Property Item(ByVal index As Integer) As Customer
    Get
    Return Me.List.Item(index)
    End Get
    Set(ByVal Value As Customer)
    Me.List.Item(index) = Value
    End Set
    End Property

    'Overloaded default property
    'Item - return email hashtable
    Default ReadOnly Property Item(ByVal email As String) As Customer
    Get
    email = email.ToLower
    Return EmailHashtable.Item(email)
    End Get
    End Property

    'add customer...
    Public Sub Add(ByVal newCustomer As Customer)
    Me.List.Add(newCustomer)

    'add to the hashtable...
    Dim useEmail As String
    useEmail = newCustomer.Email.ToLower
    EmailHashtable.Add(useEmail, newCustomer)

    End Sub

    'Remove - delete a customer...
    Public Sub Remove(ByVal removeCustomer As Customer)
    'remove from list...
    Me.List.Remove(removeCustomer)
    'remove from hashtable...
    Dim useEmail As String
    useEmail = removeCustomer.Email.ToLower
    EmailHashtable.Remove(useEmail)
    End Sub

    'Clear - provide a new implementation of clear...
    Public Shadows Sub Clear()
    'get collectionbase to clear...
    MyBase.Clear()
    'clear hashtable...
    EmailHashtable.Clear()
    End Sub

    'RemoveAt - remove an item by index...
    Public Shadows Sub RemoveAt(ByVal index As Integer)
    Remove(Item(index))
    End Sub


    End Class
    Visual Basic 6.0
    Visual Basic .NET
    C/C++
    JAVA

  9. #9
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    cool

    Magiaus

    If I helped give me some points.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width