Avatar of allelopath
allelopath
 asked on

Serializable List

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

Open in new window

Visual Basic.NET

Avatar of undefined
Last Comment
Corey Scheich

8/22/2022 - Mon
Corey Scheich

Instead of Key Value pair as the underlying type use a Structure

http://stackoverflow.com/questions/2658916/serializing-a-list-of-key-value-pairs-to-xml
SOLUTION
kaufmed

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
allelopath

ASKER
That looks like C#, not VB.
Corey Scheich

Converts pretty easily though

<Serializable> _
<XmlType(TypeName := "WhateverNameYouLike")> _
Public Structure KeyValuePair(Of K, V)
	Public Property Key() As K
		Get
			Return m_Key
		End Get
		Set
			m_Key = Value
		End Set
	End Property
	Private m_Key As K

	Public Property Value() As V
		Get
			Return m_Value
		End Get
		Set
			m_Value = Value
		End Set
	End Property
	Private m_Value As V
End Structure

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
kaufmed

@Corey2
The Serializable attribute does not affect (or effect) XML serialization--it's only for binary serialization. It doesn't break anything to include it, though.
Corey Scheich

I would agree, I just purely converted the example from the page I posted to VB.NET without tweaking it.
allelopath

ASKER
I think I have to use SOAP because a number of things are being serialized already with this. Now I've created another data structure (the SortedHashList) that must be serialized along with these.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.