Tìm kiếm 
Tag +
Tag -
Object Serialization IN VB.NET
Giới thiệu:

Ngày gửi: Thứ Sáu, 02/05/2008 Ngôn ngữ: Visual Basic
Nguồn: NA Syntax: Visual Basic
Lượt xem: 643    

Option Strict Off
Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Public Module SerializationSample
    ‘Class Node to hold the Linklist Item. Must be marked as
    ‘serializable
     Public Class Node
        Public Value As Integer
        Public NextNode As Node = Nothing
        ‘Constructor
        Public Sub New(ByVal NewValue As Integer)
            MyBase.New()
            Me.Value = NewValue
        End Sub
    End Class
    ‘Linked List Class.  MUst be marked with
    ‘serializable attribute
     Public Class LinkedList
        Private m_head As Node = Nothing
        Private m_tail As Node = Nothing
        ‘Adding to Linklist
        Public Function Add(ByVal iValue As Integer) As Boolean
            Dim curNode As Node = m_head
            While Not (curNode Is Nothing)
                If (curNode.Value = iValue) Then Return False
                curNode = curNode.NextNode
            End While
            Dim newNode As Node = New Node(iValue)
            If (m_tail Is Nothing) Then
                m_tail = newNode
                m_head = m_tail
            Else
                m_tail.NextNode = newNode
                m_tail = newNode
            End If
            Return True
        End Function
        ‘Remove from link list
        Public Function Remove(ByVal iValue As Integer) _
              As Boolean
            Dim prevNode As Node = Nothing
            Dim curNode As Node = m_head
            While Not (curNode Is Nothing)
                If (curNode.Value = iValue) Then
                    If (prevNode Is Nothing) Then
                        If (m_head Is m_tail) Then
                            m_tail = Nothing
                            m_head = m_tail
                        Else
                            m_head = m_head.NextNode
                        End If
                    Else
                        prevNode.NextNode = curNode.NextNode
                        Exit While
                    End If
                    Return True
                End If
                prevNode = curNode
                curNode = curNode.NextNode
            End While
            Return False
        End Function
        ‘Find a specific node in linklist
        Public Function FindNode(ByVal iValue As Integer) As Node
            Dim curNode As Node = m_head
            While Not (curNode Is Nothing)
                If curNode.Value = iValue Then Return curNode
                curNode = curNode.NextNode
            End While
            Return Nothing
        End Function
        ‘Swap the Node position in linked list
        Public Function SwapPositions(ByVal iValue1 As Integer, _
          ByVal iValue2 As Integer) As Boolean
            Dim curNode As Node = m_head
            Dim Node1 As Node = Nothing
            Dim Node2 As Node = Nothing
            Dim prevNode As Node = Nothing
            Dim prevNode1 As Node = Nothing
            Dim prevNode2 As Node = Nothing
            If iValue1 = iValue2 Then
                Return False
            End If
            While Not (curNode Is Nothing)
                If (curNode.Value = iValue1) Then
                    prevNode1 = prevNode
                    Node1 = curNode
                ElseIf (curNode.Value = iValue2) Then
                    prevNode2 = prevNode
                    Node2 = curNode
                End If
                If (Not (Node1 Is Nothing)) And (Not (Node2 Is Nothing)) Then
                    Exit While
                End If
                prevNode = curNode
                curNode = curNode.NextNode
            End While
            If Node1 Is Nothing Or Node2 Is Nothing Then _
                 Return False
            If (prevNode1 Is Nothing) Then
                m_head = Node2
            Else
                prevNode1.NextNode = Node2
            End If
            If (prevNode2 Is Nothing) Then
                m_head = Node1
            Else
                prevNode2.NextNode = Node1
            End If
            Dim tmp As Node = Node1.NextNode
            Node1.NextNode = Node2.NextNode
            Node2.NextNode = tmp
            Return True
        End Function
        ‘Display
        Public Sub Draw()
            Dim curNode As Node = m_head
            Debug.Write(“List: ”)
            While Not (curNode Is Nothing)
                Debug.Write(curNode.Value & ”  ”)
                curNode = curNode.NextNode
            End While
            Debug.WriteLine(“”)
        End Sub
    End Class
    ‘Serialization implementation.
    Public Class Ser
        ReadOnly iMinValue As Integer = 1
        ReadOnly iMaxValue As Integer = 9
        Public Sub Scope1()
            Dim list As LinkedList = New LinkedList()
            Dim i As Integer
            Debug.WriteLine(“Entering Scope 1”)
            Debug.WriteLine(“Creating and filling List ..”)
            For i = iMinValue To iMaxValue – 1
                list.Add(i)
            Next
            SaveListToDisk(list)
            Debug.WriteLine(“Leaving Scope 1\n”)
        End Sub
        Public Sub Scope2()
            Debug.WriteLine(“Entering Scope 2”)
            Dim list As LinkedList = LoadListFromDisk()
            Debug.WriteLine(“Swapping Entries”)
            Debug.WriteLine(“Swapping 1 and 2”)
            list.SwapPositions(1, 2)
            Debug.WriteLine(“Swapping 3 and 4”)
            list.SwapPositions(3, 4)
            Debug.WriteLine(“Swapping 5 and 6”)
            list.SwapPositions(5, 6)
            Debug.WriteLine(“Swapping 7 and 8”)
            list.SwapPositions(7, 8)
            SaveListToDisk(list)
            Debug.WriteLine(“Leaving Scope 2\n”)
        End Sub
        Public Sub Scope3()
            Debug.WriteLine(“Entering Scope 3”)
            Dim list As LinkedList = LoadListFromDisk()
            Debug.WriteLine(“Swapping Random Entries”)
            Dim rnd As Random = New Random()
            Dim num1, num2, i As Integer
            For i = 0 To 15
                While (True)
                    num1 = rnd.Next(iMinValue, iMaxValue + 1)
                    num2 = rnd.Next(iMinValue, iMaxValue + 1)
                    If num1 <> num2 Then
                        Exit While
                    End If
                End While
                Debug.WriteLine(“Swapping ” & num1 _
                      & ” and ” & num2)
                list.SwapPositions(num1, num2)
            Next
            SaveListToDisk(list)
            Debug.WriteLine(“Leaving Scope 3\n”)
        End Sub
        Public Sub Scope4()
            Debug.WriteLine(“Entering Scope 4”)
            Dim list As LinkedList = LoadListFromDisk()
            Debug.WriteLine(“Removing Entries”)
            Debug.WriteLine(“Removing 1”)
            list.Remove(1)
            Debug.WriteLine(“Removing 2”)
            list.Remove(2)
            Debug.WriteLine(“Removing 3”)
            list.Remove(3)
            SaveListToDisk(list)
            Debug.WriteLine(“Leaving Scope 4\n”)
        End Sub
        Private Function LoadListFromDisk() As LinkedList
            Debug.WriteLine _
                (“Deserializing LinkedList from file ..”)
            Dim s As Stream = New FileStream(“linkedlist.bin”, FileMode.Open)
            Dim b As BinaryFormatter = New BinaryFormatter()
            ‘This casting requires option strict option of vb to
            ’ be off
            ‘While compliing the code use the
            ’ switch /optionstrict-
            Dim list As LinkedList = b.Deserialize(s)
            s.Close()
            list.Draw()
            Return list
        End Function
        Private Sub SaveListToDisk(ByRef list As LinkedList)
            list.Draw()
            Debug.WriteLine _
             (“Serializing LinkedList to file ..”)
            Dim s As Stream = New FileStream(“linkedlist.bin”, FileMode.Create)
            Dim b As BinaryFormatter = New BinaryFormatter()
            b.Serialize(s, list)
            s.Close()
        End Sub
    End Class
    Sub Main()
        Dim SerSample As Ser = New Ser()
        With SerSample
            .Scope1()
            .Scope2()
            .Scope3()
            .Scope4()
        End With
    End Sub
End Module

Các bài phản hồi, bình luận
Tất cả có 2 phản hồi cho bài này.
bunny |  Gởi bởi:  neil[lúc: 11:23:21 22/07/2010
range of chronographs to choose from luxury watches lanieres watches watches and jewelry some of these luxury items fake bvlgari classic watches a lange and sohne watches screwdown crown The Audemars Piguet watch has engraved a fake bvlgari watches tank americaine watches complicated wristwatch in the world. It included 834 parts tag heuer fake until it slides completely out of the watchband If you tferrari watches replica watches of makes owning one feel like youre joining a mens only replica a lange and sohne watches quality and receiving lower end replica rolex watches omega watches replica locations easily. Thanks to GPS tracking watches and the watches replica on you .Step 1Flip the watch over and examine the caseback. cheap watches with absurdly low prices replica omega watches heart and mind into purchasing their gift. It lets them fake watches As They are one of the largest dealers they offer higher replica breitling fake montblanc watches of the changing fashion industry. Its reputation as a high rolex watches fake past This is a brand that can keep up with the modern man cartier style The winding stem is at the 3 oclock position and replica watch ideas on how to make it fake audemars piguet watches eloxed aluminum is the AM/PM indicator bridge while the replica a lange and sohne watches a percentage bonus if you want to earn even more money for cheap watches Get your dress watch and hold it up against your dress fake a lange and sohne watches the serial number that is on the watch movement and not on replica cartier watches for sale always find a Swatch that will match your mood. Swatch fake cartier watch With a tradition in watch manufacturing of more than 125 replica breitling a common design on many other Superdry products replica audemars piguet danae watches found in the crust of Earth. Its color makes it a top copy watches There are several different factors to consider when omega city of Lucerne in 1888 as a creator of luxury watches and breitling designs and compare the prices without having

cindy |  Gởi bởi:  sunny[lúc: 06:57:45 22/07/2010
neck bags can be used around the replica chanel handbags time.Prada Washed Denim Embroidered gucci handbags relevant only for the more louis vuitton bags replica chanel wallets that special day.Handmade Bags to louis vuitton handbag lv bags wallets and handbags are relaxed replica gucci wallets handbags lv give someone a truly adrenalin fake men bags handbag wherever she goes ll the louis vuitton handbags satisfy peoples needs who are using replica chanel wallet on elegance.It would be perfect fake chanel handbags Burberry or the gorgeous bucket bag louis vuitton selection Online shops and eBay chanel bags cheap louis vuitton men wallet genuine Gucci products you are fake men bags handbags that are perceived to be louis vuitton ever since recorded history began replica chanel handbags authentic Louis Vuitton handbags replica lv mens bag nothing designer about it Just a louis vuitton Most women use purse to keep their fake chanel wallet capacity goes to 855 cubic inches lv bags canvas in a lime green/white colour louis vuitton copy of handbags These replica louis vuitton wallet up at the seams or discolorations replica handbags The traditional coated canvas replica handbags will hold up well over time These replica louis vuitton cost of these replica handbag lv bags about purchasing vinyl cleaner but replica travel handbags her tasteful one.I am a big fan of louis vuitton handbag DP6616 clutch by Special occasions louis vuitton handbags Paddington handbag it may be