Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Variables To Eliminate Duplicate Code
 

Variables To Eliminate Duplicate Code

In an effort to reduce the amount of code duplicated with only the specific names changed I'm trying to figure out how to use variables to reference my table adapters. Unfortunately, when I try to do so, I notice that each of the tableadapter types is actually a specific name referring to a specifc table adapter created by the visual studio designers. therefore, there is no generic type that they all reside under. I'm trying to take the current code below (WhichI rezlize is a really ridiculous way of doing things with my If, THEN statements) and consolidate it into something like the secod code block below. Any help would really be appreciated. I think there's something big here that I'm missing conceptually.


Current Code:

 Private Sub Search()
            If SearchFromFormName Is CustomerForm Then
                CustomerSearchDialog.CustomerSearchReferrer = "Customer"
                CustomerSearchDialog.CustomerSearchTableAdapter.Fill(CustomerSearchDialog.TransactDataSet.CustomerSearch, "%" & SearchToolStripTextBox.Text & "%")
                If CustomerSearchDialog.TransactDataSet.CustomerSearch.Count > 0 Then
                    If CustomerSearchDialog.TransactDataSet.CustomerSearch.Count > 1 Then
                        If SearchToolStripTextBox.Text <> "" Then
                            CustomerSearchDialog.Text = "Customer Search Results for '" & SearchToolStripTextBox.Text & "'"
                        Else : CustomerSearchDialog.Text = "Customer Search Results for all Customers"
                        End If
                        CustomerSearchDialog.ShowDialog()
                    Else
                        CustomerForm.CustomerSelect(CustomerSearchDialog.CustomerSearchBindingSource.Current("CustomerID"))
                        Me.SearchToolStripTextBox.Text = ""
                    End If
                Else
                    NoResultsFound()
                End If
            ElseIf SearchFromFormName Is VendorForm Then
                VendorSearchDialog.VendorSearchReferrer = "Vendor"
                VendorSearchDialog.VendorSearchTableAdapter.Fill(VendorSearchDialog.TransactDataSet.VendorSearch, "%" & SearchToolStripTextBox.Text & "%")
                If VendorSearchDialog.TransactDataSet.VendorSearch.Count > 0 Then
                    If VendorSearchDialog.TransactDataSet.VendorSearch.Count > 1 Then
                        If SearchToolStripTextBox.Text <> "" Then
                            VendorSearchDialog.Text = "Vendor Search Results for '" & SearchToolStripTextBox.Text & "'"
                        Else : VendorSearchDialog.Text = "Vendor Search Results for all Vendors"
                        End If
                        VendorSearchDialog.ShowDialog()
                    Else
                        VendorForm.VendorSelect(VendorSearchDialog.VendorSearchBindingSource.Current("VendorID"))
                        Me.SearchToolStripTextBox.Text = ""
                    End If
                Else
                    NoResultsFound()
                End If
End If
End Sub
Example of Desired code:
    Private Sub Search2(ByVal SearchDialogName As Form, ByVal TableAdapterName As Transact.TransactDataSetTableAdapters, ByVal DataTableName As DataTable, ByVal BindingSourceName As BindingSource, ByVal FormName As Form, ByVal ID As String)
        SearchDialogName.TableAdapterName.Fill(SearchDialogName.DataTableName, "%" & SearchToolStripTextBox.Text & "%")
        If SearchDialogName.DataTableName.Count > 0 Then
            If SearchDialogName.DataTableName.Count > 1 Then
                If SearchToolStripTextBox.Text <> "" Then
                    SearchDialogName.Text = "Search Results for '" & SearchToolStripTextBox.Text & "'"
                Else : CustomerSearchDialog.Text = "Search Results for all"
                End If
                SearchDialogName.ShowDialog()
            Else
                FormName.Select(SearchDialogName.BindingSourceName.Current(ID))
                Me.SearchToolStripTextBox.Text = ""
            End If
        Else
            NoResultsFound()
        End If
    End Sub
MethodMas  Monday, September 28, 2009 3:11 AM
I think I made some progress here. Since it's the Fill method of the table adapter I'm having trouble turning into a variable, I tried to create a function property to use as the variable.
Private _SearchFunction As Func(Of DataTable, String, Integer)

 Protected Property SearchFunction() As Func(Of DataTable, String, Integer)
        Get
            Return _SearchFunction
        End Get
        Set(ByVal value As Func(Of DataTable, String, Integer))
            _SearchFunction = value
        End Set
    End Property

 SearchFunction = Me.UspCustomerSearch_SelectTableAdapter.Fill(Me.TransactDataSet.uspCustomerSearch_Select, "%" & SearchToolStripTextBox.Text & "%")
This seems like it would work in theory but when I go to set the property in the last line of code, I get the error:
Error1Value of type 'Integer' cannot be converted to 'System.Func(Of System.Data.DataTable, String, Integer)'.

So, I think I'm on the right track but am not quite there yet. If I can figure out how to set this function into a property the rest should be easy.
MethodMas  Monday, September 28, 2009 6:56 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView KeyPress Event Not Working
• HOW EXCEL GOES INTO DATABASE(MAPPEING)
• How I can make a Code for Delete this row when it loaded? (Select and delete)
• Binding a drop down list to a SQL SERVER 2005 database
• metoh with list of parameters
• DataGridView Leave Focus
• Problem with ToolStripDropDown in DataGridViewCell
• error on row deletion: using getchanges
• DataGridView Parent->Child while Filtering
• How can I get the data of row one by one from dataset