Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8283663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:48:49+00:00 2026-06-08T10:48:49+00:00

I have a class MyCol, which inherits from ObservableCollection(Of T). It overrides InsertItem method

  • 0

I have a class MyCol, which inherits from ObservableCollection(Of T). It overrides InsertItem method in such manner:

Public Event PreInsertItem As EventHandler(Of EventArgs)

Protected Overridable Sub OnPreInsertItem(e As EventAtgs)
    RaiseEvent PreInsertItem(Me, e)
End Sub

Protected Overrides Sub InsertItem(index As Integer, item As T)
    OnPreInsertItem(EventArgs.Empty)

    MyBase.InsertItem(index, item)
End Sub

As you can see I have added an event which is raised every time an item is added to MyCol collection.

Next I create another class MyColSubClass, which inherits from MyCol, and also overrides InsertItem method:

Public Overrides Sub InsertItem(index as Integer, item as T)
    OnPreInsertItem(EventArgs.Empty)

    ' some additional code goes here

    MyBase.InsertItem(index, item)
End Sub

THE QUESTION:

Now, when I use an instance of MyColSubClass and add an item, PreInsertItem event is raised twice: first in MyColSubClass and than in MyCol.

What design pattern should I use to make PreInsertItem event raise only once: in MyColSubClass?

N.B.

Classes and events, shown in examples are simplified from real life application, but assume they show exact structure of application. Raising event in last inherited class is a must.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:48:51+00:00Added an answer on June 8, 2026 at 10:48 am

    If you know for sure that the base class will raise the event, it’s useless to do so in the derived class.

    Just change your override to :

    Public Overrides Sub InsertItem(index as Integer, item as T)
        ' some additional code goes here
    
        MyBase.InsertItem(index, item)
    End Sub
    

    And that should be fine.

    But if you change your derived method, and stop calling MyBase.InsertItem(…), you should raise the event in your override to ensure it gets raised :

    Public Overrides Sub InsertItem(index as Integer, item as T)
        ' some additional code goes here
    
        OnPreInsertItem(EventArgs.Empty)
    
        ' insert your item and do whatever...
    End Sub
    

    EDIT

    If you need to change the way the event is raised, but want to make sure that it only gets raised once, simply override the OnPreInsertItem method in your derived class :

    Protected Overrides Sub OnPreInsertItem(e as EventArgs)
        ' Do wahetever you need here, change e, add info, whatever...
        ' ...
        ' Then raise the event (or call MyBase.OnPreInsertItem, as you like)
        RaiseEvent PreInsertItem(Me, e)
    End Sub
    
    Public Overrides Sub InsertItem(index as Integer, item as T)
        ' some additional code goes here
    
        ' This will work only if MyBase.InsertItem calls OnPreInsertItem. 
        ' Otherwise, you have to handle the insertion and raise the event
        ' yourself without calling the base method.
        MyBase.InsertItem(index, item)
    End Sub
    

    Since OnPreInsertItem is overridable, the version from your derived class will get called when you insert an item in your derived class.

    Hope that helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
We have class lua. In lua class there is a method registerFunc() which is
I have class which implements Countable, ArrayAccess, Iterator and Serializable. I have a public
I have class which has a method that needs to return three DataTables. I
I have class A: public class ClassA<T> Class B derives from A: public class
I have class in which I am sorting a list. import java.util.*; public class
I have Class and Student objects. Both have collection of another as property. Which
I have class with back reference: public class Employee : Entity { private string
I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have class like this below shown. which contains the shopping items where the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.