Solved

Adding an event to list(Of T)

Posted on 2013-10-22
6
201 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in page 3 44
Vb.net dynamic formulas in runtime 11 61
SQL syntax in VB.net 5 27
Get the selected ValueMember of Combobox 5 15
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

8 Experts available now in Live!

Get 1:1 Help Now