Please take a look at the code below:
Imports System.Data.SqlClient
Public Class Person
Private id As String
Private name As String
Public Function check(ByVal personid As String) As Boolean
'Do some checks on the person to see if it is ready for deletion
End Function
Public Shared Sub Delete()
Dim v As New Vehicle
Dim p As New Person
End Sub
End Class
Public Class Vehicle
Private vrm As String
Public Function check(ByVal vehicleid As String) As Boolean
'Do some checks on the vehicle to see if it is ready for deletion
End Function
Private Shared Sub Delete()
Dim p As New Person
Dim v As New Vehicle
Dim objCommand As SqlCommand
Dim objCon As SqlConnection
Dim objDR As SqlDataReader
Try
Dim _ConString As String = "Data Source=IANSCOMPUTER;Initial Catalog=Test;Integrated Security=True;MultipleActiveResultSets=true"
objCon = New SqlConnection(_ConString)
objCommand = New SqlCommand("SELECT * FROM Person were startdate < dateadd(year,-6," & Now & ")")
objDR = objCommand.ExecuteReader
Do While objDR.Read
If p.check(objDR("id")) And v.check(objDR("vehicleid")) Then
'Execute delete statement, which deletes the person and vehicle
End If
Loop
objDR.Close()
objCommand.Connection = objCon
objCon.Open()
objCommand.ExecuteNonQuery()
Catch ex As Exception
Throw
Finally
End Try
End Sub
End Class
Notice that the shared function (Person.Delete) contains references to Person and Vehicle and uses instance variables in Person and Vehicle. Basically a check needs to be done on the Person and vehicle before the person and vehicle can be deleted.
Is it poor practice to reference Person and Vehicle from the Shared function? Is it poor practice to use instance variables in the Delete function. The Delete function will delete thousands of persons and vehicles daily.
Personclass doesn't seem to do anything. What is the purpose of making theDeletesubs shared? Why are you catching an exception without handling it?