Click here to Skip to main content
15,894,540 members
Articles / Programming Languages / Visual Basic

Transactional Objects in VB.NET

Rate me:
Please Sign up or sign in to vote.
2.14/5 (6 votes)
18 Oct 20044 min read 70.5K   776   24  
An article on how to easily return objects to their prior state after editing.
'***Header************************************************************************************
'
'       Name        -  
'       Description - 
'
'       Date        -  
'       Author      -  
'
'       Modification Log
'       -------------------------------------------------------------------------------
'       ModDate     -
'       ModBy       -
'       ModDesc     -
'
'********************************************************************************************

Public Class Form1
    Inherits System.Windows.Forms.Form



#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents butNormShallow As System.Windows.Forms.Button
    Friend WithEvents butNormDeep As System.Windows.Forms.Button
    Friend WithEvents butTrans As System.Windows.Forms.Button
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents butDeepStart As System.Windows.Forms.Button
    Friend WithEvents butDeepRollback As System.Windows.Forms.Button
    Friend WithEvents butReset As System.Windows.Forms.Button
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents lblDeep As System.Windows.Forms.Label
    Friend WithEvents lblShallow As System.Windows.Forms.Label
    Friend WithEvents butShalRollback As System.Windows.Forms.Button
    Friend WithEvents butShalStart As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.butNormShallow = New System.Windows.Forms.Button
        Me.butNormDeep = New System.Windows.Forms.Button
        Me.butTrans = New System.Windows.Forms.Button
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.butReset = New System.Windows.Forms.Button
        Me.lblDeep = New System.Windows.Forms.Label
        Me.butDeepRollback = New System.Windows.Forms.Button
        Me.butDeepStart = New System.Windows.Forms.Button
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.lblShallow = New System.Windows.Forms.Label
        Me.butShalRollback = New System.Windows.Forms.Button
        Me.butShalStart = New System.Windows.Forms.Button
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.SuspendLayout()
        '
        'butNormShallow
        '
        Me.butNormShallow.Location = New System.Drawing.Point(8, 16)
        Me.butNormShallow.Name = "butNormShallow"
        Me.butNormShallow.Size = New System.Drawing.Size(120, 23)
        Me.butNormShallow.TabIndex = 0
        Me.butNormShallow.Text = "Test Normal Shallow"
        '
        'butNormDeep
        '
        Me.butNormDeep.Location = New System.Drawing.Point(8, 56)
        Me.butNormDeep.Name = "butNormDeep"
        Me.butNormDeep.Size = New System.Drawing.Size(120, 23)
        Me.butNormDeep.TabIndex = 1
        Me.butNormDeep.Text = "Test Normal Deep"
        '
        'butTrans
        '
        Me.butTrans.Location = New System.Drawing.Point(8, 96)
        Me.butTrans.Name = "butTrans"
        Me.butTrans.Size = New System.Drawing.Size(120, 24)
        Me.butTrans.TabIndex = 3
        Me.butTrans.Text = "Test Trans"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.butReset)
        Me.GroupBox1.Controls.Add(Me.lblDeep)
        Me.GroupBox1.Controls.Add(Me.butDeepRollback)
        Me.GroupBox1.Controls.Add(Me.butDeepStart)
        Me.GroupBox1.Location = New System.Drawing.Point(16, 136)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(248, 80)
        Me.GroupBox1.TabIndex = 4
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Control Test Deep"
        '
        'butReset
        '
        Me.butReset.Font = New System.Drawing.Font("Microsoft Sans Serif", 6.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.butReset.Location = New System.Drawing.Point(160, 56)
        Me.butReset.Name = "butReset"
        Me.butReset.Size = New System.Drawing.Size(40, 16)
        Me.butReset.TabIndex = 7
        Me.butReset.Text = "Reset"
        '
        'lblDeep
        '
        Me.lblDeep.Location = New System.Drawing.Point(136, 32)
        Me.lblDeep.Name = "lblDeep"
        Me.lblDeep.Size = New System.Drawing.Size(80, 16)
        Me.lblDeep.TabIndex = 6
        Me.lblDeep.Text = "Start Text"
        Me.lblDeep.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'butDeepRollback
        '
        Me.butDeepRollback.Location = New System.Drawing.Point(8, 48)
        Me.butDeepRollback.Name = "butDeepRollback"
        Me.butDeepRollback.Size = New System.Drawing.Size(112, 24)
        Me.butDeepRollback.TabIndex = 5
        Me.butDeepRollback.Text = "Rollback"
        '
        'butDeepStart
        '
        Me.butDeepStart.Location = New System.Drawing.Point(8, 24)
        Me.butDeepStart.Name = "butDeepStart"
        Me.butDeepStart.Size = New System.Drawing.Size(112, 24)
        Me.butDeepStart.TabIndex = 4
        Me.butDeepStart.Text = "Start"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.Add(Me.lblShallow)
        Me.GroupBox2.Controls.Add(Me.butShalRollback)
        Me.GroupBox2.Controls.Add(Me.butShalStart)
        Me.GroupBox2.Location = New System.Drawing.Point(16, 216)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(248, 80)
        Me.GroupBox2.TabIndex = 5
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "Control Test Shallow"
        '
        'lblShallow
        '
        Me.lblShallow.Location = New System.Drawing.Point(144, 40)
        Me.lblShallow.Name = "lblShallow"
        Me.lblShallow.Size = New System.Drawing.Size(80, 16)
        Me.lblShallow.TabIndex = 6
        Me.lblShallow.Text = "Start Text"
        Me.lblShallow.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'butShalRollback
        '
        Me.butShalRollback.Location = New System.Drawing.Point(8, 48)
        Me.butShalRollback.Name = "butShalRollback"
        Me.butShalRollback.Size = New System.Drawing.Size(112, 24)
        Me.butShalRollback.TabIndex = 5
        Me.butShalRollback.Text = "Rollback"
        '
        'butShalStart
        '
        Me.butShalStart.Location = New System.Drawing.Point(8, 24)
        Me.butShalStart.Name = "butShalStart"
        Me.butShalStart.Size = New System.Drawing.Size(112, 24)
        Me.butShalStart.TabIndex = 4
        Me.butShalStart.Text = "Start"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 309)
        Me.Controls.Add(Me.GroupBox2)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.butTrans)
        Me.Controls.Add(Me.butNormDeep)
        Me.Controls.Add(Me.butNormShallow)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region


    '----------------------------------------------------------------------------
    'This shows how to use an existing object with the Transaction functions, no 
    'changes to the base class are required.
    'The important aspects here are the use of the Shallow style of the transaction.
    'We see that the code in the Property is called. As well we see that the Public 
    'Variable is not changed by the rollback. We also see that the Collection 
    'object is only copied as a reference. 
    'Additonaly we could note that were the property Read or Write only, that the 
    'underlying variable would not be touched
    '
    'Uses: NormClass
    '----------------------------------------------------------------------------

    Private Sub butNormShallow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butNormShallow.Click
        Dim sOut As String = ""
        Dim oSTran As New Transaction.ShallowTrans
        Dim oObj As New NormalClass
        Dim bSet As Boolean
        Dim bGet As Boolean
        Dim sItems As String = ""

        oObj.PubVar = "Public"
        oObj.Value = "val"
        oObj.PubCol.Add("aa", "aa")
        sOut = [String].Concat("Before Shallow Transaction", vbCrLf, "PublicVar: ", oObj.PubVar, vbCrLf)
        sOut = [String].Concat(sOut, "Property: ", oObj.Value, vbCrLf)
        sOut = [String].Concat(sOut, "Col Members: ")
        For Each sStr As String In oObj.PubCol
            If sItems <> "" Then
                sItems = [String].Concat(sItems, ", ")
            End If
            sItems = [String].Concat(sItems, sStr)
        Next
        sOut = [String].Concat(sOut, sItems, vbCrLf, vbCrLf)
        sItems = ""

        'Reset the flags for having called the Set/Get
        oObj.Reset()

        oSTran.BeginTrans(oObj, New NormalClass)
        'Get the set and get flags before we touch the property
        bSet = oObj.SetCalled
        bGet = oObj.GetCalled

        oObj.PubVar = "Modified"
        oObj.Value = "mod"
        oObj.PubCol.Add("bb", "bb")

        sOut = [String].Concat(sOut, "During Shallow Transaction", vbCrLf)
        sOut = [String].Concat(sOut, "PublicVar: ", oObj.PubVar, vbCrLf)
        sOut = [String].Concat(sOut, "Property: ", oObj.Value, vbCrLf)
        sOut = [String].Concat(sOut, "Col Members: ")
        For Each sStr As String In oObj.PubCol
            If sItems <> "" Then
                sItems = [String].Concat(sItems, ", ")
            End If
            sItems = [String].Concat(sItems, sStr)
        Next
        sOut = [String].Concat(sOut, sItems, vbCrLf)
        sOut = [String].Concat(sOut, "SetCalled: ", bSet.ToString, vbCrLf)
        sOut = [String].Concat(sOut, "GetCalled: ", bGet.ToString, vbCrLf, vbCrLf)

        sItems = ""

        'Reset the flags for having called the Set/Get
        oObj.Reset()

        oSTran.RollbackTrans()

        'Get the set and get flags before we read the property
        bSet = oObj.SetCalled
        bGet = oObj.GetCalled

        sOut = [String].Concat(sOut, "After Shallow Rollback", vbCrLf)
        sOut = [String].Concat(sOut, "PublicVar: ", oObj.PubVar, vbCrLf)
        sOut = [String].Concat(sOut, "Property: ", oObj.Value, vbCrLf)
        sOut = [String].Concat(sOut, "Col Members: ")
        For Each sStr As String In oObj.PubCol
            If sItems <> "" Then
                sItems = [String].Concat(sItems, ", ")
            End If
            sItems = [String].Concat(sItems, sStr)
        Next
        sOut = [String].Concat(sOut, sItems, vbCrLf)
        sOut = [String].Concat(sOut, "SetCalled: ", bSet.ToString, vbCrLf)
        sOut = [String].Concat(sOut, "GetCalled: ", bGet.ToString, vbCrLf)

        MsgBox(sOut)
    End Sub

    '----------------------------------------------------------------------------
    'This shows how to use an existing object with the Transaction functions, no 
    'changes to the base class are required.
    'The important aspects here are the use of the Deep style of the transaction.
    'We see that the code in the Property is not called. As well we see that the 
    'Public Variable is changed by the rollback. We also see that the Collection 
    'object is only copied as a reference. 
    'Additionaly we could see that the read/write status of a property is 
    'immaterial as we are accessing the underlying variables directly.
    '
    'Use: NormClass
    '----------------------------------------------------------------------------
    Private Sub butNormDeep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butNormDeep.Click
        Dim sOut As String = ""
        Dim oSTran As New Transaction.DeepTrans
        Dim oObj As New NormalClass
        Dim bSet As Boolean
        Dim bGet As Boolean
        Dim sItems As String = ""

        oObj.PubVar = "Public"
        oObj.Value = "val"
        oObj.PubCol.Add("aa", "aa")
        sOut = [String].Concat("Before Deep Transaction", vbCrLf, "PublicVar: ", oObj.PubVar, vbCrLf)
        sOut = [String].Concat(sOut, "Property: ", oObj.Value, vbCrLf)
        sOut = [String].Concat(sOut, "Col Members: ")
        For Each sStr As String In oObj.PubCol
            If sItems <> "" Then
                sItems = [String].Concat(sItems, ", ")
            End If
            sItems = [String].Concat(sItems, sStr)
        Next
        sOut = [String].Concat(sOut, sItems, vbCrLf, vbCrLf)
        sItems = ""

        'Reset the flags for having called the Set/Get
        oObj.Reset()

        oSTran.BeginTrans(oObj, New NormalClass)
        'Get the set and get flags before we touch the property
        bSet = oObj.SetCalled
        bGet = oObj.GetCalled

        oObj.PubVar = "Modified"
        oObj.Value = "mod"
        oObj.PubCol.Add("bb", "bb")

        sOut = [String].Concat(sOut, "During Deep Transaction", vbCrLf)
        sOut = [String].Concat(sOut, "PublicVar: ", oObj.PubVar, vbCrLf)
        sOut = [String].Concat(sOut, "Property: ", oObj.Value, vbCrLf)
        sOut = [String].Concat(sOut, "Col Members: ")
        For Each sStr As String In oObj.PubCol
            If sItems <> "" Then
                sItems = [String].Concat(sItems, ", ")
            End If
            sItems = [String].Concat(sItems, sStr)
        Next
        sOut = [String].Concat(sOut, sItems, vbCrLf)
        sOut = [String].Concat(sOut, "SetCalled: ", bSet.ToString, vbCrLf)
        sOut = [String].Concat(sOut, "GetCalled: ", bGet.ToString, vbCrLf, vbCrLf)

        'Reset the flags for having called the Set/Get
        oObj.Reset()
        sItems = ""

        oSTran.RollbackTrans()

        'Get the set and get flags before we read the property
        bSet = oObj.SetCalled
        bGet = oObj.GetCalled

        sOut = [String].Concat(sOut, "After Deep Rollback", vbCrLf)
        sOut = [String].Concat(sOut, "PublicVar: ", oObj.PubVar, vbCrLf)
        sOut = [String].Concat(sOut, "Property: ", oObj.Value, vbCrLf)
        sOut = [String].Concat(sOut, "Col Members: ")
        For Each sStr As String In oObj.PubCol
            If sItems <> "" Then
                sItems = [String].Concat(sItems, ", ")
            End If
            sItems = [String].Concat(sItems, sStr)
        Next
        sOut = [String].Concat(sOut, sItems, vbCrLf)
        sOut = [String].Concat(sOut, "SetCalled: ", bSet.ToString, vbCrLf)
        sOut = [String].Concat(sOut, "GetCalled: ", bGet.ToString, vbCrLf)

        MsgBox(sOut)
    End Sub

    '----------------------------------------------------------------------------
    'This shows how to use an object that implements the TransBase interface. 
    'This is shown as a DeepTrans action to show the full capabilities. 
    'As well in in the underlying Object, we have replaced the standard collection 
    'with a TransactionCollection and thus we can see how this collection does 
    'roll back the changes. 
    'Were we to place in the Transaction collection other objects that implemented 
    'the TransBase interface we would as well see these rollback.
    '
    'Uses: TransClass
    '----------------------------------------------------------------------------
    Private Sub butTrans_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butTrans.Click
        Dim sOut As String = ""
        Dim oObj As New TransClass
        Dim bSet As Boolean
        Dim bGet As Boolean
        Dim sItems As String = ""

        oObj.PubVar = "Public"
        oObj.Value = "val"
        oObj.PubCol.Add("aa", "aa")
        sOut = [String].Concat("Before Deep Transaction", vbCrLf, "PublicVar: ", oObj.PubVar, vbCrLf)
        sOut = [String].Concat(sOut, "Property: ", oObj.Value, vbCrLf)
        sOut = [String].Concat(sOut, "Col Members: ")
        For Each sStr As String In oObj.PubCol
            If sItems <> "" Then
                sItems = [String].Concat(sItems, ", ")
            End If
            sItems = [String].Concat(sItems, sStr)
        Next
        sOut = [String].Concat(sOut, sItems, vbCrLf, vbCrLf)
        sItems = ""

        'Reset the flags for having called the Set/Get
        oObj.Reset()

        oObj.BeginTrans()

        'Get the set and get flags before we touch the property
        bSet = oObj.SetCalled
        bGet = oObj.GetCalled

        oObj.PubVar = "Modified"
        oObj.Value = "mod"
        oObj.PubCol.Add("bb", "bb")

        sOut = [String].Concat(sOut, "During Deep Transaction", vbCrLf)
        sOut = [String].Concat(sOut, "PublicVar: ", oObj.PubVar, vbCrLf)
        sOut = [String].Concat(sOut, "Property: ", oObj.Value, vbCrLf)
        sOut = [String].Concat(sOut, "Col Members: ")
        For Each sStr As String In oObj.PubCol
            If sItems <> "" Then
                sItems = [String].Concat(sItems, ", ")
            End If
            sItems = [String].Concat(sItems, sStr)
        Next
        sOut = [String].Concat(sOut, sItems, vbCrLf)
        sOut = [String].Concat(sOut, "SetCalled: ", bSet.ToString, vbCrLf)
        sOut = [String].Concat(sOut, "GetCalled: ", bGet.ToString, vbCrLf, vbCrLf)

        'Reset the flags for having called the Set/Get
        oObj.Reset()
        sItems = ""

        oObj.Rollback()

        'Get the set and get flags before we read the property
        bSet = oObj.SetCalled
        bGet = oObj.GetCalled

        sOut = [String].Concat(sOut, "After Deep Rollback", vbCrLf)
        sOut = [String].Concat(sOut, "PublicVar: ", oObj.PubVar, vbCrLf)
        sOut = [String].Concat(sOut, "Property: ", oObj.Value, vbCrLf)
        sOut = [String].Concat(sOut, "Col Members: ")
        For Each sStr As String In oObj.PubCol
            If sItems <> "" Then
                sItems = [String].Concat(sItems, ", ")
            End If
            sItems = [String].Concat(sItems, sStr)
        Next
        sOut = [String].Concat(sOut, sItems, vbCrLf)
        sOut = [String].Concat(sOut, "SetCalled: ", bSet.ToString, vbCrLf)
        sOut = [String].Concat(sOut, "GetCalled: ", bGet.ToString, vbCrLf)

        MsgBox(sOut)

    End Sub

    '----------------------------------------------------------------------------
    'This shows a DeepTrans Class works with a Control .. not at all
    '----------------------------------------------------------------------------
    Private m_oDeepTrn As Transaction.DeepTrans

    Private Sub butDeepStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butDeepStart.Click
        m_oDeepTrn = New Transaction.DeepTrans
        m_oDeepTrn.BeginTrans(lblDeep, New System.Windows.Forms.Label)

        lblDeep.Text = "Modified"
        lblDeep.ForeColor = System.Drawing.Color.Red
    End Sub

    Private Sub butDeepRollback_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butDeepRollback.Click
        m_oDeepTrn.RollbackTrans()
        MsgBox("Rolled back")
        m_oDeepTrn = Nothing
    End Sub

    Private Sub butReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butReset.Click
        lblDeep.Text = "Start Text"
        lblDeep.ForeColor = System.Drawing.Color.Black
    End Sub

    '----------------------------------------------------------------------------
    'This shows how a ShallowTrans Class works with a Control .. perfect
    '----------------------------------------------------------------------------
    Private m_oShalTrn As Transaction.ShallowTrans

    Private Sub butShalStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butShalStart.Click
        m_oShalTrn = New Transaction.ShallowTrans
        m_oShalTrn.BeginTrans(lblShallow, New System.Windows.Forms.Label)

        lblShallow.Text = "Modified"
        lblShallow.ForeColor = System.Drawing.Color.Red
    End Sub

    Private Sub butShalRollback_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butShalRollback.Click
        m_oShalTrn.RollbackTrans()
        m_oShalTrn = Nothing
    End Sub


End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions