Tip

Use inheritance on forms for simpler and more robust source code

This tip was submitted to the VS.NET Info Center by member Edwin Steenstra. Please let other users know how useful it is by rating it below. Do you have a tip or code of your own you'd like to share? Submit it here.


By using inheritance, source code can be made simpler and more robust. In this case, I'm dealing with stored procedures to write the contents of various forms to SQL Server. By using inherited forms from a form called BaseTable, in which all actions are predefined, I only have to worry about the parameters of the command object.

Code:

Public Class BaseTable
    Inherits System.Windows.Forms.Form
'
'Just the piece of code for saving an item is given
'
    Protected Overridable Sub AddPutParameters(ByVal Cmd As SqlClient.SqlCommand)
    End Sub


    Public Sub SaveItem()

        Dim Success As Boolean = True

        Try
            Dim Cmd As New SqlClient.SqlCommand()
            Cmd.Connection = New SqlClient.SqlConnection(_connectionString)
            Cmd.Connection.Open()
            Cmd.CommandType = CommandType.StoredProcedure
            Cmd.Parameters.Add(New SqlClient.SqlParameter("@id", SqlDbType.UniqueIdentifier))
            Cmd.Parameters("@id").Direction = ParameterDirection.Output
            AddPutParameters(Cmd)
            Cmd.ExecuteNonQuery()
        Catch ex As Exception
            Success = False
        End Try

        If Success Then
            HandleSuccess
        End If

    End Sub

End Class

To save a record in the derived forms you just have to implement the parameters that are specific to the table the form handles.

Public Class frm_COUNTRY

    Inherits BaseTable

    Protected Overrides Sub AddPutParameters(ByVal Cmd As SqlClient.SqlCommand)
        Cmd.Parameters.Add("@name", _name.Text)
        Cmd.Parameters.Add("@code", _code.Text)
        Cmd.CommandText="_spPUT_COUNTRY"

    End Sub

This was first published in May 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.