Solved

Adding an event to list(Of T)

Posted on 2013-10-22
6
200 Views
Last Modified: 2013-10-30
Is there a means to add an event to generic.list(Of T)?
0
Comment
Question by:Alyanto
  • 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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
 

Author Closing Comment

by:Alyanto
ID: 39611508
Excellent, thank you.

/Aly
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now