473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Add an event handler to an arraylist

I have looked high and low for an example of adding an eventhandler to
an arraylist. I want a method to fire every time a item is added to
an arraylist. Any sample code would be appreciated.

IE. Dim myArrayList as new Arraylist
myArrayList.add("StringData") ---------->EventFires

Msgbox("A Record has been added.")

Thanks,
Peter
Nov 21 '05 #1
4 1840
You can't add an event handler the way you've mentioned. However, you can
inherit from the ArrayList class and add an event which you can raise when
an item is added. Here's how:

Public Class ArrayListEx
Inherits ArrayList

Public Event ItemAdded(ByVal Index As Integer)

Public Overrides Function Add(ByVal value As Object) As Integer
Dim ReturnValue As Integer = MyBase.Add(value)
RaiseEvent ItemAdded(ReturnValue)
Return ReturnValue
End Function
End Class

Private WithEvents oArr As New ArrayListEx

Private Sub TestSub()
oArr.Add(123)
oArr.Add("test")
End Sub

Private Sub oArr_ItemAdded(ByVal Index As Integer) Handles oArr.ItemAdded
MessageBox.Show("Index of Item Added: " & Index)
End Sub

Now just call TestSub from anywhere and you'll see a message box pop up
whenever an item is added to the ArrayList.

hope that helps..
Imran.

"Peter" <pe***@mclinn.com> wrote in message
news:dc**************************@posting.google.c om...
I have looked high and low for an example of adding an eventhandler to
an arraylist. I want a method to fire every time a item is added to
an arraylist. Any sample code would be appreciated.

IE. Dim myArrayList as new Arraylist
myArrayList.add("StringData") ---------->EventFires

Msgbox("A Record has been added.")

Thanks,
Peter

Nov 21 '05 #2
Peter,

Peter, there can in my opinion only be one reason when you want to use an
event on an array and that is when you have binded something (in whatever
way) to that array using a sealed class.

That will be a lot of work, and the sample from Imran shows how, but using
the datatable in that case give that direct.

An other reason can be that someone lost the control over his code. However
using it than leads more and more to spagetti code.

Therefore I am curious in what situation you want to use this?

Cor
"Peter" <pe***@mclinn.com> schreef in bericht
news:dc**************************@posting.google.c om...
I have looked high and low for an example of adding an eventhandler to
an arraylist. I want a method to fire every time a item is added to
an arraylist. Any sample code would be appreciated.

IE. Dim myArrayList as new Arraylist
myArrayList.add("StringData") ---------->EventFires

Msgbox("A Record has been added.")

Thanks,
Peter

Nov 21 '05 #3
I have multiple threads writing to a public arraylist. The additions
come back in different intervals (Data Queries against routers). When
data comes back I update another form with the results.

I ened up adding a raiseevent right after the .add function. This
seems to work great. I'm still open for better solutions though.

-Peter
Nov 21 '05 #4
Peter,

In my opinion is than the event from the router. That gives you than as well
the information to add to the arraylist. In my opinion will that even make
it possible when you pass the information using that event to do it without
a synclock, however this is just a suggestion (idea).

Cor

"Peter" <pe***@mclinn.com>
..com...
I have multiple threads writing to a public arraylist. The additions
come back in different intervals (Data Queries against routers). When
data comes back I update another form with the results.

I ened up adding a raiseevent right after the .add function. This
seems to work great. I'm still open for better solutions though.

-Peter

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dips | last post by:
Python Gurus, Is it possible to write python WMI event consumer using event handler rather than polling WMI? I got the equivalent C# code snippet from...
7
by: Alberto | last post by:
I have a class A who send an event when an instance is modified. So, I declared the event in this way: public delegate void Evento(object sender); public event Evento Modified; To throw the...
2
by: FredC | last post by:
S Name Microsoft Windows XP Professional Version 5.1.2600 Service Pack 2 Build 2600 Total Physical Memory 1,024.00 MB MDE 2003 Version 7.1.3008 ..NET Framework 1.1 Version 1.1.4322 SP1...
1
by: MuZZy | last post by:
Hi, Is there a way to remove all event handlers for a control's event? Say, i have a button and i want to remove all button.Click events for it - i don't know how many of them was hooked to the...
1
by: Grant | last post by:
The Idea: Page1.aspx sets a property value to an arraylist. Page2.aspx retreives that property (using a Context.Handler) every time the page loads. It then iterates through the arraylist and...
3
by: Peter Oliphant | last post by:
In general, some controls can have events handlers attached to them, typically via the += operator. How does one remove an event handler added this way? Or, just as good for my purposes, how can...
1
by: Jeff Evans | last post by:
Hi, I was wondering if it's possible to remove a Control c from a Controls collection in the parent's event handler for an event fired by c. For instance let's say we have public class...
1
by: Marek Murin | last post by:
Hi all, I have created vb.net user control that has to be used by vb6 form. Everything goes well with putting the vb.net user control on the VB6 form until I want to receive any event from my...
1
by: Armin Zingler | last post by:
Hi, I add event handlers to different events of objects of different type. In an array or arraylist, I want to store the information about which events I added. Later, I want to process the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.