Imports System.Collections.Generic Imports System.Runtime.Serialization.Formatters.Binary Imports System.IO <Serializable()> _ Public Class Personne Private _nom As String Private _prenom As String Private _age As Integer Public Property Nom As String Get Return _nom End Get Set(ByVal value As String) _nom = value End Set End Property Public Property Prenom As String Get Return _prenom End Get Set(ByVal value As String) _prenom = value End Set End Property Public Property Age As Integer Get Return _age End Get Set(ByVal value As Integer) _age = value End Set End Property Public Sub New(ByVal nom As String, ByVal prenom As String, ByVal age As Integer) _nom = nom _prenom = prenom _age = age End Sub End Class Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim formatter As New BinaryFormatter() Dim personnes As New SortedList(Of String, Personne) Dim unePersonne As Personne 'Création d'une personne unePersonne = New Personne("Durant", "Pierre", 30) 'Ajout de la personne à la liste personnes.Add(unePersonne.Nom, unePersonne) 'ou directement personnes.Add("Dupond", New Personne("Dupond", "Jacques", 20)) 'Accès à l'age d'une personne Console.WriteLine("L'age de Mr. Durant est {0}", personnes("Durant").Age) 'Modification de l'age d'une personne personnes("Dupond").Age = 22 'Modification du nom d'une personnes unePersonne = personnes("Durant") personnes.Remove("Durant") unePersonne.Nom = "Durand" personnes.Add(unePersonne.Nom, unePersonne) 'Sérialization de la liste de personnes Using fs As New FileStream("C:\maListe", FileMode.Create, FileAccess.Write) formatter.Serialize(fs, personnes) End Using 'Déserialization de la liste de personnes Dim personnesDeserialize As SortedList(Of String, Personne) Using fs As New FileStream("C:\maListe", FileMode.Open, FileAccess.Read) personnesDeserialize = TryCast(formatter.Deserialize(fs), SortedList(Of String, Personne)) End Using If (personnesDeserialize IsNot Nothing) Then 'Iteration de la liste des personnes déserializée For Each kvp As KeyValuePair(Of String, Personne) In personnesDeserialize Console.WriteLine("Clé unique {0} ; Nom {1} ; Prénom = {2} ; Age = {3}", _ kvp.Key, _ kvp.Value.Nom, _ kvp.Value.Prenom, _ kvp.Value.Age) Next 'Suppression d'une personne personnesDeserialize.Remove("Dupond") Else Console.WriteLine("Le fichier 'C:\maListe' n'est pas une liste de personnes valide") End If End Sub
'Déserialization de la liste de personnes Using fs As New FileStream("C:\maListe", FileMode.Open, FileAccess.Read) personnes = TryCast(formatter.Deserialize(fs), SortedList(Of String, Personne)) End Using
si je supprime "Durand", cela laisse une case vide dans mon tableau et m'oblige à decaler toutes les entrées aprés l'Entrée "Durand".
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPublic Structure Personne Public Prenom as String Public Age as Integer Public Sub New(ByVal prenom as String, ByVal Age as Integer) Me.Prenom = prenom Me.Age = age End Sub End Structure Public Sub Toto() Dim personnes as New System.Collections.Generic.SortedList(Of String, Personne) 'Ajout personnes.Add("Durant", new Personne("Pierre", 30)) 'Utilisation Console.WriteLine("Prénom de Mr.Durant = ", personnes("Durant").Prenom) Console.WriteLine("Agede Mr.Durant = ", personnes("Durant").Age) 'Par contre le nom étant unique, pour le modifier il faudra faire Dim durant as Personne = personnes("Durant") personnes.Remove("Durant") personnes.Add("Durand", durant) 'Suppression personnes.Remove("Durant") End Sub
For Each personne as System.Collections.Generic.KeyValuePair(Of String, Personne) In personnes Console.WriteLine("Nom {0} ; Prénom {1} ; Age = {2}", personne.Key, personne.Value.Prenom, personne.Value.Age) Next
1 - Pour assigner une valeur du style :
personnes("Durant").Age = 30 'Ca ne marche pas
personnes("Durant") = New Personne(personnes("Durant").Prenom, 30)