Results 1 to 2 of 2

Thread: Delegate?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Delegate?

    Here is the scenario:

    I have a form that uses a Typed DataSet that contains 9 tables. In my Save routine, I go through each DataTable by name and look at whether items were added or editted and call inserts and updates accordingly. I would prefer to pass this information into a subroutine since it is so repetitive, but how do I pass the name of the insert or edit routine from my InsertData and UpdateData class modules to another routine.

    For example, this section of code is the same, just different datatables in use. However, each datatable has a different Insert or Update routine.
    VB Code:
    1. If Not ds.Patients.GetChanges(DataRowState.Added) Is Nothing Then
    2.           iData.InsertPatient(ds.Patients.GetChanges(DataRowState.Added), myTrans)
    3.      End If
    4.  
    5.      If Not ds.Patients.GetChanges(DataRowState.Modified) Is Nothing Then
    6.           uData.UpdatePatient(ds.Patients.GetChanges(DataRowState.Modified), myTrans)
    7.      End If

    I would like to do something like:
    VB Code:
    1. Private Sub SaveData()
    2.           ' Declarations and other code up here
    3.  
    4.           Call CheckForAdditionsAndPost(ds.Patients, myTrans, "InsertPatient")
    5.           Call CheckForModificationsAndPost(ds.Patients, myTrans, "UpdatePatient")
    6.      End Sub
    7.  
    8.      Private Sub CheckForAdditionsAndPost(ByRef inDataTable as DataTable, ByRef inTrans as SqlTransaction, byval inProcedure as String)
    9.      Dim iData as New InsertData
    10.  
    11.      If Not inDataTable.GetChanges(DataRowState.Added) Is Nothing Then
    12.           iData.  [COLOR=Sienna]Eval(inProcedure).[/COLOR]   (inDataTable.GetChanges(DataRowState.Added), inTrans)
    13.      End If
    14.  
    15.      ' Cleanup
    16.      iData.Dispose()
    17.      End Sub


    Some example code would be greatly appreciated!!

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: Delegate?

    Maybe I have figured it out. Here is what I have. Let me know if there is a better way to do this.

    VB Code:
    1. Private Delegate Sub InsertUpdateData(ByRef inDataTable As DataTable, ByRef inTrans As System.Data.SqlClient.SqlTransaction)
    2.  
    3.  
    4.  
    5.     Private Sub SaveData
    6.             Dim myDelegate As InsertUpdateData
    7.  
    8.                 ' Check for Additions to Contacts
    9.                 myDelegate = AddressOf iData.InsertContact
    10.                 Call CheckForAdditionsAndPost(ds.ContactsL1.GetChanges(DataRowState.Added), myTrans, iPatient_ID, myDelegate)
    11.                 Call CheckForAdditionsAndPost(ds.ContactsR1.GetChanges(DataRowState.Added), myTrans, iPatient_ID, myDelegate)
    12.                 Call CheckForAdditionsAndPost(ds.ContactsL2.GetChanges(DataRowState.Added), myTrans, iPatient_ID, myDelegate)
    13.                 Call CheckForAdditionsAndPost(ds.ContactsR2.GetChanges(DataRowState.Added), myTrans, iPatient_ID, myDelegate)
    14.  
    15.                 ' Check for Updates to Contacts
    16.                 myDelegate = AddressOf uData.UpdateContact
    17.                 Call CheckForUpdatesAndPost(ds.ContactsL1.GetChanges(DataRowState.Modified), myTrans, iPatient_ID, myDelegate)
    18.                 Call CheckForUpdatesAndPost(ds.ContactsR1.GetChanges(DataRowState.Modified), myTrans, iPatient_ID, myDelegate)
    19.                 Call CheckForUpdatesAndPost(ds.ContactsL2.GetChanges(DataRowState.Modified), myTrans, iPatient_ID, myDelegate)
    20.                 Call CheckForUpdatesAndPost(ds.ContactsR2.GetChanges(DataRowState.Modified), myTrans, iPatient_ID, myDelegate)
    21.     End Sub
    22.  
    23.  
    24.     Private Sub CheckForAdditionsAndPost(ByRef inDataTable As DataTable, ByRef inTrans As System.Data.SqlClient.SqlTransaction, ByRef inPatient_ID As Int32, ByRef inDelegate As InsertUpdateData)
    25.         If Not inDataTable Is Nothing Then
    26.             Dim dr As DataRow
    27.  
    28.             ' Set the Patient_ID for each row in the Insert
    29.             For Each dr In inDataTable.Rows
    30.                 dr("Patient_ID") = inPatient_ID
    31.             Next
    32.  
    33.             BindingContext(inDataTable).EndCurrentEdit()
    34.             inDelegate.Invoke(inDataTable, inTrans)
    35.         End If
    36.     End Sub
    37.  
    38.     Private Sub CheckForUpdatesAndPost(ByRef inDataTable As DataTable, ByRef inTrans As System.Data.SqlClient.SqlTransaction, ByRef inDelegate As InsertUpdateData)
    39.         If Not inDataTable Is Nothing Then
    40.             inDelegate.Invoke(inDataTable, inTrans)
    41.         End If
    42.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width