Link to home
Start Free TrialLog in
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

Avatar of Corey Scheich
Corey Scheich
Flag of United States of America image

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of allelopath
allelopath

ASKER

That looks like C#, not VB.
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

@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.
I would agree, I just purely converted the example from the page I posted to VB.NET without tweaking it.
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial