Solved

Adding an event to list(Of T)

Posted on 2013-10-22
6
205 Views
Last Modified: 2013-10-30
Is there a means to add an event to generic.list(Of T)?
0
Comment
Question by:Alyanto
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 39590595
Yes, but you will have to create your own custom class to handle the events...

For example, this code will show a message box with the newly added item when the button is clicked.
Public Class Form1

    Private WithEvents myList As MyCustomList

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        myList = New MyCustomList

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        myList.Add("new item")
    End Sub

    Private Sub myList_ItemAdded(ByRef sender As System.Collections.Generic.List(Of String), ByRef item As String) Handles myList.ItemAdded
        MessageBox.Show("New item added: " & item)
    End Sub

End Class

Public Class MyCustomList
    Inherits Generic.List(Of String)

    Public Sub New()
        MyBase.New()
    End Sub

    Public Event ItemAdded(ByRef sender As List(Of String), ByRef item As String)

    Public Overloads Sub Add(item As String)
        MyBase.Add(item)
        RaiseEvent ItemAdded(Me, item)
    End Sub

End Class

Open in new window

0
 
LVL 1

Author Comment

by:Alyanto
ID: 39591082
Is there a way to make this more generic, I can see from the code that it will only be a list of string.

Would it be possible to
                 
 Public Class MyCustomList
    Inherits Generic.List(Of T)

    Public Sub New()
        MyBase.New()
    End Sub

    Public Event ItemAdded(ByRef sender As List(Of String), ByRef item As String)

    Public Overloads Sub Add(item As String)
        MyBase.Add(item)
        RaiseEvent ItemAdded(Me, item)
    End Sub

End Class

Open in new window

perhaps?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39591569
Can you describe what you are trying to achieve?
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 1

Author Comment

by:Alyanto
ID: 39593352
In the very simplest sense I am trying to improve generic.list or similar so that when something is added or deleted from the list an event fires.  

This is a generic piece of code with no specific goal but has a general application.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39594525
right. Does following work in making it generic?

 Public Class MyCustomList
    Inherits Generic.List(Of T)

    Public Sub New()
        MyBase.New()
    End Sub

    Public Event ItemAdded(ByRef sender As List(Of T), ByRef item As T)

    Public Overloads Sub Add(item As T)
        MyBase.Add(item)
        RaiseEvent ItemAdded(Me, item)
    End Sub

End Class

Open in new window

0
 
LVL 1

Author Closing Comment

by:Alyanto
ID: 39611508
Excellent, thank you.

/Aly
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question