Board index » Visual Studio » Handling List(Of T) Events in Custom Class

Handling List(Of T) Events in Custom Class

Visual Studio217
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?


-
 

Re:Handling List(Of T) Events in Custom Class

What is showSite()? Is that the 'internal' code you want to execute. If not

then what is the code you want to execute?





"Bryan" <bryanvick@gmail.com>wrote in message

Quote
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?





-

Re:Handling List(Of T) Events in Custom Class

On Feb 21, 10:05 pm, "Stephany Young" <noone@localhost>wrote:

Quote
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.



-

Re:Handling List(Of T) Events in Custom Class

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" <bryanvick@gmail.com>wrote in message

Quote
On Feb 21, 10:05 pm, "Stephany Young" <noone@localhost>wrote:

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





-

Re:Handling List(Of T) Events in Custom Class

On Feb 21, 10:25 pm, "Stephany Young" <noone@localhost>wrote:

Quote
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



-

Re:Handling List(Of T) Events in Custom Class

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" <bryanvick@gmail.com>wrote in message

Quote
On Feb 21, 10:25 pm, "Stephany Young" <noone@localhost>wrote:

>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





-

Re:Handling List(Of T) Events in Custom Class

On Feb 21, 11:12 pm, "Stephany Young" <noone@localhost>wrote:

Quote
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.



-