troubleshooting Question

Serializable List

Avatar of allelopath
allelopath asked on
Visual Basic.NET
8 Comments2 Solutions482 ViewsLast Modified:
How would I make the class below Serializable? I think I would need a SoapFormatter.

Imports System.Collections.Generic ' ' List

Public Class SortedHashList
    Inherits List(Of KeyValuePair(Of String, Object))

    Private Class KeyComparer
        Implements IComparer(Of KeyValuePair(Of String, Object))
        Public Function Compare(ByVal pair1 As System.Collections.Generic.KeyValuePair(Of String, Object), _
                                              ByVal pair2 As System.Collections.Generic.KeyValuePair(Of String, Object)) As Integer _
                            Implements System.Collections.Generic.IComparer(Of System.Collections.Generic.KeyValuePair(Of String, Object)).Compare
            Dim comparison As Integer
            comparison = pair2.Key.CompareTo(pair1.Key)
            ''Console.WriteLine("My strings: string1: " & pair1.Key & vbTab & "string2: " & pair2.Key & vbTab & "comparison: " & comparison)

            Return comparison
        End Function
    End Class

    Private Class ValueComparer
        Implements IComparer(Of KeyValuePair(Of String, Object))
        Public Function Compare(ByVal pair1 As System.Collections.Generic.KeyValuePair(Of String, Object), _
                                ByVal pair2 As System.Collections.Generic.KeyValuePair(Of String, Object)) As Integer _
                            Implements System.Collections.Generic.IComparer(Of System.Collections.Generic.KeyValuePair(Of String, Object)).Compare
            ''Console.WriteLine("My strings: string1: " & pair1.Value & vbTab & "string2: " & pair2.Value)

            Return pair2.Value.CompareTo(pair1.Value)
        End Function
    End Class

    Public Overloads Sub Add(ByVal key As String, ByVal value As Object)
        MyBase.Add(New KeyValuePair(Of String, Object)(key, value))
    End Sub


    Public Sub SortByKey()
        MyBase.Sort(New KeyComparer)
    End Sub


    Public Sub SortByValue()
        MyBase.Sort(New ValueComparer)
    End Sub

    ''' <summary>
    ''' loop through list looking for key
    ''' </summary>
    ''' <returns>
    ''' return true if found, false otherwise
    ''' </returns>
    ''' <remarks></remarks>
    Public Function ContainsKey(ByVal keyToLookFor As String) As Boolean

        Dim foundKey As Boolean
        foundKey = False

        If Not Me Is Nothing Then
            Dim kvp As KeyValuePair(Of String, Object)
            For Each kvp In Me
                Dim key As String
                key = kvp.Key
                If (key.Equals(keyToLookFor)) Then
                    foundKey = True
                    Continue For
                End If
            Next

        End If

        Return foundKey

    End Function

    ''' <summary>
    ''' loop through list looking for key
    ''' </summary>
    ''' <returns>
    ''' return true if found, false otherwise
    ''' </returns>
    ''' <remarks></remarks>
    Public Overloads Function Remove(ByVal keyToRemove As String) As Boolean

        Dim foundKey As Boolean
        foundKey = False

        If Not Me Is Nothing Then
            Dim kvp As KeyValuePair(Of String, Object)
            For Each kvp In Me
                Dim key As String
                key = kvp.Key
                If (key.Equals(keyToRemove)) Then
                    Me.Remove(kvp)
                    foundKey = True
                    Continue For
                End If
            Next

        End If

        Return foundKey

    End Function

    ''' <summary>
    ''' loop through list looking for key
    ''' </summary>
    ''' <returns>
    ''' return Key/Value pair of key
    ''' </returns>
    ''' <remarks></remarks>
    Public Overloads Function GetObject(ByVal key As String) As Object

        Dim foundKey As Boolean
        foundKey = False

        If Not Me Is Nothing Then
            Dim kvp As KeyValuePair(Of String, Object)
            For Each kvp In Me
                Dim currentKey As String
                currentKey = kvp.Key
                If (currentKey.Equals(key)) Then
                    Return kvp
                    foundKey = True
                    Continue For
                End If
            Next

        End If

        Return foundKey

    End Function


    ''' <summary>
    ''' loop through list looking for key
    ''' </summary>
    ''' <returns>
    ''' return true if found, false otherwise
    ''' </returns>
    ''' <remarks></remarks>
    Public Overloads Function GetValue(ByVal key As String) As String

        Dim foundKey As Boolean
        foundKey = False

        If Not Me Is Nothing Then
            Dim kvp As KeyValuePair(Of String, Object)
            For Each kvp In Me
                Dim currentKey As String
                currentKey = kvp.Key
                If (currentKey.Equals(key)) Then
                    Return kvp.Value
                    foundKey = True
                    Continue For
                End If
            Next

        End If

        Return foundKey

    End Function

    ''' <summary>
    ''' loop through list looking for key
    ''' </summary>
    ''' <returns>
    ''' return true if found, false otherwise
    ''' </returns>
    ''' <remarks></remarks>
    Public Overloads Function Put(ByVal key As String, ByVal value As Object) As Boolean

        Dim foundKey As Boolean
        foundKey = False

        If Not Me Is Nothing Then
            Dim kvp As KeyValuePair(Of String, Object)
            For Each kvp In Me
                Dim currentKey As String
                currentKey = kvp.Key
                If (currentKey.Equals(key)) Then
                    Me.Put(key, value)
                    foundKey = True
                    Continue For
                End If
            Next

        End If

        Return foundKey

    End Function

End Class
ASKER CERTIFIED SOLUTION
Corey Scheich
Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros