Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Adding an event to list(Of T)

Posted on 2013-10-22
6
Medium Priority
?
210 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 48

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

604 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