RandomAccess file for storing the custom objects : Structure Serialization « Class Module « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Class Module » Structure Serialization 
6.48.3.RandomAccess file for storing the custom objects
Imports System.IO

public class Test
   public Shared Sub Main
        Dim emp As New Employee

        Dim file_num As Integer = FreeFile()

        FileOpen(file_num, "MYFILE.DAT", OpenMode.Random, _
             OpenAccess.ReadWrite, OpenShare.Shared, _
             Len(emp))

        FilePut(file_num, New Employee(1"A""A"))
        FilePut(file_num, New Employee(2"B""B"))
        FilePut(file_num, New Employee(3"C""C"))

        Dim obj As ValueType = DirectCast(emp, ValueType)
        For Each i As Integer In New Integer() {31,  2}
            FileGet(file_num, obj, i)
            emp = DirectCast(obj, Employee)
            Console.WriteLine(emp.ToString())
        Next i

        FileClose(file_num)
   End Sub
End class
    Public Structure Employee
        Public ID As Integer
        <VBFixedString(15)Public FirstName As String
        <VBFixedString(15)Public LastName As String

        Public Sub New(ByVal new_id As Integer, ByVal first_name As String, _
         ByVal last_name As String)
            ID = new_id
            FirstName = first_name
            LastName = last_name
        End Sub

        Public Overrides Function ToString() As String
            Return ID & ": " & FirstName & " " & LastName
        End Function
    End Structure
3: C               C
1: A               A
2: B               B
6.48.Structure Serialization
6.48.1.Save Structure to a binary file
6.48.2.Read Record (Structure) from binary file
6.48.3.RandomAccess file for storing the custom objects
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.