Click here to Skip to main content
15,891,762 members
Articles / Programming Languages / Visual Basic

Refactoring - elixir of youth for legacy VB code

Rate me:
Please Sign up or sign in to vote.
4.85/5 (45 votes)
3 Dec 2004CPOL22 min read 146.6K   307   73  
Standard refactoring techniques coupled with automated refactoring tool provide excellent platform for legacy VB code upgrade. Legacy VB code suffers poor structure and bloated code due to lack of inheritance and other OO capabilities.
Option Strict Off
Option Explicit On 

Imports System.Runtime.InteropServices

Namespace TeslaTeam.RefactoringVB.RefactoredECCFramework


    <ProgIdAttribute("ColAccount_NET.ColAccount"), SerializableAttribute()> _
        Public MustInherit Class EntityCollection
        Inherits CollectionBase
        Implements ISecured

        'reference to AccountPersistence instanca we are to delegate methods to
        Protected mPersistence As EntityPersistence

        ' Returns an item from the Collection or returns Nothing.
        Public ReadOnly Property Item(ByVal Index As Integer) As Entity
            Get
                Dim mintCodeID As Short
                If InnerList Is Nothing Then
                    Exit Property
                End If

                If InnerList.Count() = 0 Then
                    Exit Property
                End If
                'maintain compatibility (Underlying ArrayList 0 based)
                Item = InnerList.Item(Index - 1)
            End Get
        End Property

        ' Fills the Collection. Returns True or False.
        Public Function Load(ByVal SecurityToken As String, ByVal FilledStorage As ArrayList) As Boolean
            Dim entity As Entity
            Load = False
            mPersistence.SecurityToken = SecurityToken
            InnerList.Clear()
            For Each entity In FilledStorage
                InnerList.Add(entity)
            Next
            Load = True
        End Function

        ' Removes an item in the Collection if Index Key passed in or else it calls
        ' Clear. Returns True or False.
        Public Function Remove(Optional ByVal Index As Integer = IS_MISSING_OPTIONAL_PARAM_INT) As Boolean

            Remove = False

            If IsMissing(Index) Then
                Me.Clear()
                Remove = True
            End If

            If (Index < 1 Or Index > Me.Count) Then
                Err.Raise(ERR_ACCOUNTINDEXOUTOFRANGE, "ColAccount.Remove PROC", "Remove Failed: Index out of range.")
            End If

            InnerList.Remove((Index))
            Remove = True
        End Function


        Public Function Add(ByVal entity As Entity) As Integer
            InnerList.Add(entity)
            Add = Me.Count()
        End Function

        ' delegates
        Public Function Delete(Optional ByVal Index As Integer = IS_MISSING_OPTIONAL_PARAM_INT) As Boolean
            If Not IsMissing(Index) Then
                Delete = mPersistence.Delete(Index)
            Else
                Delete = mPersistence.Delete()
            End If
        End Function

        ' This method marks an item for deletion if an Index Key is passed in OR it marks
        ' all items if it is not passed. It does this by setting the DeleteFlag. You must
        ' call Update() to presist this information to the database. Returns True or
        ' False.
        Public Function MarkForDelete(Optional ByVal Index As Integer = IS_MISSING_OPTIONAL_PARAM_INT) As Boolean
            Dim LowerLimit As Integer
            Dim UpperLimit As Integer
            Dim inx As Integer

            MarkForDelete = False

            If Not IsMissing(Index) Then
                'Check Index
                If (Index < 1 Or Index > Me.Count) Then
                    Err.Raise(ERR_ACCOUNTINDEXOUTOFRANGE, "ColAccount.MarkForDelete PROC", "MarkForDelete Failed: Index out of range.")
                End If
                'Toggle DeleteFlag
                Me.Item(Index).DeleteFlag = True
            Else
                LowerLimit = 1
                UpperLimit = Me.Count
                For inx = LowerLimit To UpperLimit
                    Me.Item(inx).DeleteFlag = True
                Next
            End If
            MarkForDelete = True
        End Function

        ' UnDeletes an item in the Collection. Returns True or False.    
        Public Function Undelete(Optional ByVal Index As Integer = IS_MISSING_OPTIONAL_PARAM_INT) As Boolean

            Dim LowerLimit As Integer
            Dim UpperLimit As Integer
            Dim inx As Integer

            Undelete = False

            'Check Index
            If Not IsMissing(Index) Then
                If (Index < 1 Or Index > InnerList.Count) Then
                    Err.Raise(ERR_ACCOUNTINDEXOUTOFRANGE, "ColAccount.UnDelete PROC", "UnDelete Failed: Index out of range.")
                End If

                'Toggle DeleteFlag
                Me.Item(Index).DeleteFlag = False
            Else
                LowerLimit = 1
                UpperLimit = InnerList.Count
                For inx = LowerLimit To UpperLimit
                    Me.Item(inx).DeleteFlag = False
                Next
            End If
            Undelete = True
        End Function

        ' delegates
        Public Function Update(Optional ByVal Index As Integer = IS_MISSING_OPTIONAL_PARAM_INT) As Boolean
            If Not IsMissing(Index) Then
                Update = mPersistence.Update(Index)
            Else
                Update = mPersistence.Update
            End If
        End Function

        ' delegates
        Friend Property SecurityToken() As String Implements ISecured.SecurityToken
            Get
                SecurityToken = mPersistence.SecurityToken
            End Get
            Set(ByVal Value As String)
                mPersistence.SecurityToken = Value
            End Set
        End Property

    End Class

End Namespace

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Chile Chile
Danijel Arsenovski is senior developer and consultant from Santiago, Chile. His interests include advanced OO programming techniques and refactoring. He holds Microsoft's Solution Developer Certification and is often speaker at Microsoft's technical conferences. Recently, he has been recognized as Microsoft MVP.
He is the author of book "Professional Refactoring in Visual Basic" and "Professional Refactoring in C# and ASP .NET" from Wrox.
From time to time he blogs at http://blog.refactoringin.net

Comments and Discussions