Imports System.IO
Imports System.Xml.Serialization
MustInherit Class Contact
Dim _Id As Guid
Dim _Nom As String = ""
Dim _Prenom As String = ""
Dim _Adresse As String = ""
Dim _Telephone As String = ""
Dim _Gsm As String = ""
Dim _Fax As String = ""
Dim _Mail As String = ""
Public ReadOnly Property Id As Guid
Get
_Id = New Guid
End Get
End Property
Public Property Nom(ByVal _Nom As String) As String
Get
Return _Nom
End Get
Set(value As String)
_Nom = value
End Set
End Property
Public Property Prenom(ByVal _Prenom As String) As String
Get
Return _Prenom
End Get
Set(value As String)
_Prenom = value
End Set
End Property
Public Property Adresse(ByVal _Adresse As String) As String
Get
Return _Adresse
End Get
Set(value As String)
_Adresse = value
End Set
End Property
Public Property Telephone(ByVal _Telephone As String) As String
Get
Return _Telephone
End Get
Set(value As String)
_Telephone = value
End Set
End Property
Public Property Gsm(ByVal _Gsm As String) As String
Get
Return _Gsm
End Get
Set(value As String)
_Gsm = value
End Set
End Property
Public Property Fax(ByVal _Fax As String) As String
Get
Return _Fax
End Get
Set(value As String)
_Fax = value
End Set
End Property
Public Property Mail(ByVal _Mail As String) As String
Get
Return _Mail
End Get
Set(value As String)
_Mail = value
End Set
End Property
Sub New(ByVal Nom As String, ByVal Prenom As String, ByVal Adresse As String, ByVal Telephone As String, ByVal Gsm As String, ByVal Fax As String, ByVal Mail As String)
Me._Nom = Nom
Me._Prenom = Prenom
Me._Adresse = Adresse
Me._Telephone = Telephone
Me._Gsm = Gsm
Me._Fax = Fax
Me._Mail = Mail
End Sub
Private Function Serialisation()
Dim MonFichier As IO.FileStream = New IO.FileStream("Monrepertoire.xml", FileMode.OpenOrCreate)
Dim Malisteclient As New List(Of Contact)
Dim MyWriter As New System.IO.StreamWriter("Monrepertoire.xml")
Dim Ser As New XmlSerializer(GetType(Contact))
Ser.Serialize(MonFichier, MyWriter)
End Function
Private Function Deserialisation()
End Function
End Class
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionj'ai pensé que serialiser et deserialiser est une methode interneok alors autant écrire une méthode alors, pas une classe.
Quand j'ouvre mon interface elle déserialise les données pour les afficher.A la fermeture au contraire je serialise pour sauvegarderet du coup ton IHM doit d'une façon ou d'une autre signaler à la classe qu'elle doit lancer sa méthode privée... au final une méthode public est plus simple.
J'ai créer une class MustInherite pour avoir l'occasion d'utiliser une classe abstraitemais du coup ta méthode privée est encore moins accessible.
Imports System.IO
Imports System.Xml.Serialization
Public Class Contact
Dim MonFichier As IO.FileStream = New IO.FileStream("Monrepertoire.xml", FileMode.OpenOrCreate)
Dim Malisteclient As New List(Of Contact)
Dim _Id As Guid
Dim _Nom As String = ""
Dim _Prenom As String = ""
Dim _Adresse As String = ""
Dim _Telephone As String = ""
Dim _Gsm As String = ""
Dim _Fax As String = ""
Dim _Mail As String = ""
Public ReadOnly Property Id As Guid
Get
_Id = New Guid
End Get
End Property
Public Property Nom(ByVal _Nom As String) As String
Get
Return _Nom
End Get
Set(value As String)
_Nom = value
End Set
End Property
Public Property Prenom(ByVal _Prenom As String) As String
Get
Return _Prenom
End Get
Set(value As String)
_Prenom = value
End Set
End Property
Public Property Adresse(ByVal _Adresse As String) As String
Get
Return _Adresse
End Get
Set(value As String)
_Adresse = value
End Set
End Property
Public Property Telephone(ByVal _Telephone As String) As String
Get
Return _Telephone
End Get
Set(value As String)
_Telephone = value
End Set
End Property
Public Property Gsm(ByVal _Gsm As String) As String
Get
Return _Gsm
End Get
Set(value As String)
_Gsm = value
End Set
End Property
Public Property Fax(ByVal _Fax As String) As String
Get
Return _Fax
End Get
Set(value As String)
_Fax = value
End Set
End Property
Public Property Mail(ByVal _Mail As String) As String
Get
Return _Mail
End Get
Set(value As String)
_Mail = value
End Set
End Property
Sub New(ByVal Nom As String, ByVal Prenom As String, ByVal Adresse As String, ByVal Telephone As String, ByVal Gsm As String, ByVal Fax As String, ByVal Mail As String)
Me._Nom = Nom
Me._Prenom = Prenom
Me._Adresse = Adresse
Me._Telephone = Telephone
Me._Gsm = Gsm
Me._Fax = Fax
Me._Mail = Mail
End Sub
Public Sub Serialisation()
Dim MyWriter As New System.IO.StreamWriter("Monrepertoire.xml")
Dim Ser As New XmlSerializer(GetType(Contact))
Ser.Serialize(MonFichier, MyWriter)
End Sub
Private Function Deserialisation() As List(Of Contact)
Dim connexion As New SqlClient.SqlConnection
Dim MyData As New DataSet
Dim MyReader As Xml.XmlReader
connexion.Open()
While MyReader.Read()
MyData.ReadXml(MonFichier)
End While
Return Malisteclient
End Function
End Class
Public Sub Serialisation() Dim MyWriter As New System.IO.StreamWriter("Monrepertoire.xml") Dim Ser As New XmlSerializer(GetType(Contact)) Ser.Serialize(MonFichier, MyWriter) End Sub
Dim contactBook As New List(Of Person) Dim jean As Person =... ' inutile de s'appesantir sur l'initialisation des objets... Dim jacques As Person =... contactBoox.Add(jean) contactBoox.Add(jacques) Dim xs As New XmlSerializer(GetType(List(Of Person))) Using wr As New StreamWriter("contactBook.xml") xs.Serialize(wr, contactBook) End Using
Imports System.IO</code>
Imports System.Xml.Serialization
Public Class Contact
Dim MonFichier As IO.FileStream = New IO.FileStream("Monrepertoire.xml", FileMode.OpenOrCreate)
Dim Malisteclient As New List(Of Contact)
Private _Id As String = System.Guid.NewGuid.ToString
Private _Nom As String = ""
Private _Prenom As String = ""
Private _Adresse As String = ""
Private _Telephone As String = ""
Private _Gsm As String = ""
Private _Fax As String = ""
Private _Mail As String = ""
Public Property Id(ByVal _Id As String) As String
Get
Return _Id
End Get
Set(value As String)
_Id = value
End Set
End Property
Public Property Nom(ByVal _Nom As String) As String
Get
Return _Nom
End Get
Set(value As String)
_Nom = value
End Set
End Property
Public Property Prenom(ByVal _Prenom As String) As String
Get
Return _Prenom
End Get
Set(value As String)
_Prenom = value
End Set
End Property
Public Property Adresse(ByVal _Adresse As String) As String
Get
Return _Adresse
End Get
Set(value As String)
_Adresse = value
End Set
End Property
Public Property Telephone(ByVal _Telephone As String) As String
Get
Return _Telephone
End Get
Set(value As String)
_Telephone = value
End Set
End Property
Public Property Gsm(ByVal _Gsm As String) As String
Get
Return _Gsm
End Get
Set(value As String)
_Gsm = value
End Set
End Property
Public Property Fax(ByVal _Fax As String) As String
Get
Return _Fax
End Get
Set(value As String)
_Fax = value
End Set
End Property
Public Property Mail(ByVal _Mail As String) As String
Get
Return _Mail
End Get
Set(value As String)
_Mail = value
End Set
End Property
Public Sub New(ByVal Id As String, ByVal Nom As String, ByVal Prenom As String, ByVal Adresse As String, ByVal Telephone As String, ByVal Gsm As String, ByVal Fax As String, ByVal Mail As String)
Me._Id = Id
Me._Nom = Nom
Me._Prenom = Prenom
Me._Adresse = Adresse
Me._Telephone = Telephone
Me._Gsm = Gsm
Me._Fax = Fax
Me._Mail = Mail
End Sub
Public Sub Serialisation(ByVal Data As Data.DataTable)
Try
If IsNothing(Data) OrElse Data.Rows.Count = 0 Then
Exit Sub
End If
Data.WriteXml(MonFichier)
Catch ex As Exception
Throw
End Try
End Sub
Public Function Deserialisation() As System.Data.DataTable
Dim MyDataSet As New DataSet
Dim MyDataadapter As New SqlClient.SqlDataAdapter()
Dim MyReader As New IO.StreamReader(MonFichier)
If IO.File.Exists("Monrepertoire.xml") Then
MyDataSet.ReadXml(MyReader, XmlReadMode.InferSchema)
Dim MyDataTable As New DataTable
If Not MyDataSet Is Nothing AndAlso MyDataSet.Tables.Count = 1 Then
MyDataTable = MyDataSet.Tables(0).Copy
End If
If Not IsNothing(MyReader) Then
MyReader.Close()
MyReader.Dispose()
End If
Return MyDataTable
Else
Return Nothing
End If
End Function
Public Function ChargerContact() As System.Collections.Generic.List(Of Contact)
Dim ListesDesContacts As New List(Of Contact)
Dim MyDataContacts As Data.DataTable = Deserialisation()
If Not MyDataContacts Is Nothing Then
For Each MyDatarowContact As Data.DataRow In MyDataContacts.Rows
Dim MonContact As New Contact(MyDatarowContact("Contact_Id"), _
MyDatarowContact("Contact_Nom"), _
MyDatarowContact("Contact_Prenom"), _
MyDatarowContact("Contact_Adresse"), _
MyDatarowContact("Contact_Telephone"), _
MyDatarowContact("Contact_Gsm"), _
MyDatarowContact("Contact_Fax"), _
MyDatarowContact("Contact_Mail"))
ListesDesContacts.Add(MonContact)
Next
End If
Return ListesDesContacts
End Function
End Class<code basic>
24 févr. 2015 à 13:56