Change the Key for an Object in a Collection

Change the Key for an Object in a Collection

I have created a generic function for changing the key for an object in a collection:

 'Module: Module1    Option Explicit    Public Function ChangeKey(Object As Object, NewKey As String, Collection As Collection) As Boolean    Dim Item As Object    Dim Index As Long        For Each Item In Collection            Index = Index + 1            If Item Is Object Then                If Len(NewKey) Then                    Collection.Add Object, NewKey, , Index                Else                    Collection.Add Object, , , Index                End If                Collection.Remove Index                ChangeKey = True                Exit For            End If        Next    End Function'Class: Class1'Collection procedures not used in this example has been removed.     Option Explicit        Private mCol As Collection        Public Function Add(Optional Key As String) As Class2    Dim objNewMember As Class2        Set objNewMember = New Class2                objNewMember.Load Key, mCol                If Len(Key) = 0 Then            mCol.Add objNewMember        Else            mCol.Add objNewMember, Key        End If            Set Add = objNewMember        Set objNewMember = Nothing    End Function        Public Sub Clear()        Do While mCol.Count            mCol.Remove 1        Loop    End Sub        Public Property Get Item(Index As Variant) As Class2        Set Item = mCol(Index)    End Property        Private Sub Class_Initialize()        Set mCol = New Collection    End Sub        Private Sub Class_Terminate()        Clear        Set mCol = Nothing    End Sub'Class: Class2    Option Explicit        Private mKey As String    Private mCol As Collection        Friend Sub Load(Key As String, Col As Collection)        mKey = Key        Set mCol = Col    End Sub        Public Property Get Key() As String        Key = mKey    End Property    Public Property Let Key(vData As String)        If mKey <> vData Then            If ChangeKey(Me, vData, mCol) Then                mKey = vData            Else                Err.Raise vbObjectError + 1, TypeName(Me) & ".Key", "Failed to change key"            End If        End If    End Property'Form: Form1' * Add an Comand ButtonOption ExplicitPrivate Sub Command1_Click()Dim Col As Class1Dim Item1 As Class2Dim Item2 As Class2    Set Col = New Class1        Set Item1 = Col.Add()    Item1.Key = "Test 1"    Set Item1 = Col.Item("Test 1")        Set Item2 = Col.Add("Test2")    Item2.Key = "Test 2"    Set Item2 = Col.Item("Test 2")End Sub

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing