By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,651 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,651 IT Pros & Developers. It's quick & easy.

Handling List(Of T) Events in Custom Class

P: n/a
I am writing a class that has a list of strings as one of its
properties. I want to be able to run some code whenever that list of
strings is modified through the class property. Here is the
definition of the property 'JobSites'

Public Class SiteList
Private _JobSites As New List(Of String)
Public Property JobSites() As List(Of String)
Get
Return _JobSites
showSite()
End Get
Set(ByVal value As List(Of String))
_JobSites = value
showSite()
End Set
End Property
End Class

Whenever I add or remove a string from the JobSites list I want to be
able to process the results, for example

Dim Test as new SiteList
Test.JobSites.Add("Example 1") 'At this point, I want the object to
run some code internally

Do I override the List's Add method? Have not had any luck trying to
declare the list as withevents and then overriding the methods that
alter the list. Any suggestions?

Feb 22 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
What is showSite()? Is that the 'internal' code you want to execute. If not
then what is the code you want to execute?
"Bryan" <br*******@gmail.comwrote in message
news:11*********************@s48g2000cws.googlegro ups.com...
>I am writing a class that has a list of strings as one of its
properties. I want to be able to run some code whenever that list of
strings is modified through the class property. Here is the
definition of the property 'JobSites'

Public Class SiteList
Private _JobSites As New List(Of String)
Public Property JobSites() As List(Of String)
Get
Return _JobSites
showSite()
End Get
Set(ByVal value As List(Of String))
_JobSites = value
showSite()
End Set
End Property
End Class

Whenever I add or remove a string from the JobSites list I want to be
able to process the results, for example

Dim Test as new SiteList
Test.JobSites.Add("Example 1") 'At this point, I want the object to
run some code internally

Do I override the List's Add method? Have not had any luck trying to
declare the list as withevents and then overriding the methods that
alter the list. Any suggestions?
Feb 22 '07 #2

P: n/a
On Feb 21, 10:05 pm, "Stephany Young" <noone@localhostwrote:
What is showSite()? Is that the 'internal' code you want to execute. If not
then what is the code you want to execute?
ShowSite() is something that must run if certain properties in the
object are changed. Yes it has the code that I want to run when an
item is added to the JobSite string list.

Feb 22 '07 #3

P: n/a
I really meant ...

What is the code inside showSite?

Where is showSite located?

You say it is 'something' that must run ... but is it THE code you refer to
in your OP or is there othe code that must run as well?
"Bryan" <br*******@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
On Feb 21, 10:05 pm, "Stephany Young" <noone@localhostwrote:
>What is showSite()? Is that the 'internal' code you want to execute. If
not
then what is the code you want to execute?

ShowSite() is something that must run if certain properties in the
object are changed. Yes it has the code that I want to run when an
item is added to the JobSite string list.
Feb 22 '07 #4

P: n/a
On Feb 21, 10:25 pm, "Stephany Young" <noone@localhostwrote:
I really meant ...

What is the code inside showSite?

Where is showSite located?

You say it is 'something' that must run ... but is it THE code you refer to
in your OP or is there othe code that must run as well?
ShowSite() is a private sub in the custom class. It handles some GUI
stuff. I call it when certain properties change that affect the GUI.
I need it to run after the JobSites string list is modified in any
way. Right now it will only change if I directly access the JobSites
property, i.e.
Object.JobSites = new List(Of String)
or
dim x as List(Of String) = Object.JobSites

I need it to run when I do:
Object.JobSites.Add("Site 1")
or
Object.JobSites.Remove("Site 2")
etc

Feb 22 '07 #5

P: n/a
By 'custom class' I take it that you mean SiteList?

If so then it is something like:

Public Class SiteList

Private _JobSites As New List(Of String)

Public Sub New()

showSite()

End Sub

Public Sub Add(ByVal value As String)

_JobSites.Add(value)

showSite()

End Sub

Public Function Remove(ByVal value As String) As Boolean

Dim _result as Boolean = _JobSites.Remove(value)

showSite()

Return _result

End Sub

Public Property JobSites() As List(Of String)

Get
Return _JobSites
End Get

Set(ByVal value As List(Of String))
_JobSites = value
showSite()
End Set

End Property

Private Sub showSite()

...

End Sub

End Class
"Bryan" <br*******@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...
On Feb 21, 10:25 pm, "Stephany Young" <noone@localhostwrote:
>I really meant ...

What is the code inside showSite?

Where is showSite located?

You say it is 'something' that must run ... but is it THE code you refer
to
in your OP or is there othe code that must run as well?

ShowSite() is a private sub in the custom class. It handles some GUI
stuff. I call it when certain properties change that affect the GUI.
I need it to run after the JobSites string list is modified in any
way. Right now it will only change if I directly access the JobSites
property, i.e.
Object.JobSites = new List(Of String)
or
dim x as List(Of String) = Object.JobSites

I need it to run when I do:
Object.JobSites.Add("Site 1")
or
Object.JobSites.Remove("Site 2")
etc
Feb 22 '07 #6

P: n/a
On Feb 21, 11:12 pm, "Stephany Young" <noone@localhostwrote:
By 'custom class' I take it that you mean SiteList?

If so then it is something like:

Public Class SiteList

Private _JobSites As New List(Of String)

Public Sub New()

showSite()

End Sub

Public Sub Add(ByVal value As String)

_JobSites.Add(value)

showSite()

End Sub

Public Function Remove(ByVal value As String) As Boolean

Dim _result as Boolean = _JobSites.Remove(value)

showSite()

Return _result

End Sub

Public Property JobSites() As List(Of String)

Get
Return _JobSites
End Get

Set(ByVal value As List(Of String))
_JobSites = value
showSite()
End Set

End Property

Private Sub showSite()

...

End Sub

End Class
I ended up creating a new class that inherits List(Of String), and
added a custom event called ListChanged that fires whenever add,
delete, clear etc are called. Then my SiteList object can react to
that event. I chose this route because I didn't want to have to
expose every method of List(Of String) through my SiteList object.
Thanks for the help everyone.

Feb 22 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.