Sep
13
Written by:
Steve Gray
9/13/2010 4:59 PM
All the examples that I find show how to delete from a List (of String), but my objects are rarely that simple. Here’s how to delete from a more complicated structure.
In this project, we’re dealing with SQL Server databases, so we start with a class that defines one database.
Public Class Target
Public ServerInstance As String
Public Database As String
Sub New(ByVal strServerInstance As String, ByVal strDatabase As String)
ServerInstance = strServerInstance
Database = strDatabase
End Sub
End Class
Then we have a class that contains a List (of Target). This class also has the ‘deleteTarget’ method. Populate a ‘target’ object and pass it to ‘deleteTarget’. We’ll look for something that matches this.
We need a public ‘findTarget’ for the ‘myFind’ routine to look at.
FindIndex returns the index of any matching list entry, and RemoveAt removes the item from the List
Public Class Profiles
Public CurrentProfile
Public ScriptsFolder
Public Targets As New List(Of Target)
Public findTarget As Target
Sub deleteTarget(ByVal t As Target)
findTarget = t
Dim i As Int16 = Me.Targets.FindIndex(AddressOf myfind)
Me.Targets.RemoveAt(i)
End Sub
Function myfind(ByVal t As Target) As Boolean
If t.Database = findTarget.Database And t.ServerInstance = findTarget.ServerInstance Then
Return True
Else
Return False
End If
End Function
End Class
As always, I welcome your comments!