Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to serialize an object as an INI file using VB.NET
Message
De
24/01/2014 12:21:41
 
 
À
24/01/2014 12:12:39
Information générale
Forum:
ASP.NET
Catégorie:
SOAP
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2000 Server
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01592379
Message ID:
01592403
Vues:
49
>>Something like this example may be all you need (I've used a Dictionary rather than a basic collection - more efficient, better type safety):
>>
Imports System.Collections.Generic
>>Imports System.IO
>>
>>Namespace ConsoleApplication3
>>	Class Program
>>		Private Shared Sub Main(args As String())
>>			Dim userA = New User() With { _
>>				Key .Name = "Gary", _
>>				Key .OperatorName = "Gary NiceGuy", _
>>				Key .ID = "Gary", _
>>				Key .AccessLevel = 4, _
>>				Key .Password = "50564348" _
>>			}
>>			Dim userB = New User() With { _
>>				Key .Name = "Tester", _
>>				Key .OperatorName = "Tester Hester", _
>>				Key .ID = "Tester", _
>>				Key .AccessLevel = 1, _
>>				Key .Password = "435242455243" _
>>			}
>>			Dim users = New Users()
>>			users.Add(userA.Name, userA)
>>			users.Add(userB.Name, userB)
>>
>>			Using sw As New StreamWriter("test.ini")
>>				For Each v As var In users
>>					sw.WriteLine("[" & v.Key & "]")
>>					sw.WriteLine("OperatorID=" & v.Value.ID)
>>					sw.WriteLine("OperatorName=" & v.Value.OperatorName)
>>					sw.WriteLine("AccessLevel=" & v.Value.AccessLevel.ToString())
>>					sw.WriteLine("OperatorPassword" & v.Value.Password)
>>					sw.WriteLine()
>>				Next
>>			End Using
>>		End Sub
>>	End Class
>>
>>	Public Class User
>>		Public Property ID() As String
>>			Get
>>				Return m_ID
>>			End Get
>>			Set
>>				m_ID = Value
>>			End Set
>>		End Property
>>		Private m_ID As String
>>		Public Property Name() As String
>>			Get
>>				Return m_Name
>>			End Get
>>			Set
>>				m_Name = Value
>>			End Set
>>		End Property
>>		Private m_Name As String
>>		Public Property OperatorName() As String
>>			Get
>>				Return m_OperatorName
>>			End Get
>>			Set
>>				m_OperatorName = Value
>>			End Set
>>		End Property
>>		Private m_OperatorName As String
>>		Public Property Password() As String
>>			Get
>>				Return m_Password
>>			End Get
>>			Set
>>				m_Password = Value
>>			End Set
>>		End Property
>>		Private m_Password As String
>>		Public Property LoggedIn() As [Boolean]
>>			Get
>>				Return m_LoggedIn
>>			End Get
>>			Set
>>				m_LoggedIn = Value
>>			End Set
>>		End Property
>>		Private m_LoggedIn As [Boolean]
>>		Public Property AccessLevel() As Integer
>>			Get
>>				Return m_AccessLevel
>>			End Get
>>			Set
>>				m_AccessLevel = Value
>>			End Set
>>		End Property
>>		Private m_AccessLevel As Integer
>>	End Class
>>
>>	Public Class Users
>>		Inherits Dictionary(Of String, User)
>>
>>	End Class
>>End Namespace
>
>Thank you very much for your help on this.
>
>The next question that I have is how I would go about using serialization and deserialization on an INI file.

The example above would 'serialize' in the format you want. De-serializing is trickier (especially if you didn't write the ini file in the first place)
There's a good example of a custom text serializer/deserializer here : http://stackoverflow.com/questions/6293884/custom-serialization-of-an-object-in-net
It's C# (you try using http://www.developerfusion.com/tools/convert/csharp-to-vb/ to convert to VB) and has a few frills you may not need.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform