IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

SortedList protégé contre l'écriture [Débutant]


Sujet :

VB.NET

  1. #1
    Membre averti
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mars 2012
    Messages
    640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Bâtiment

    Informations forums :
    Inscription : Mars 2012
    Messages : 640
    Points : 372
    Points
    372
    Par défaut SortedList protégé contre l'écriture
    Bonjour à tous,
    J'ai une classe Parente qui expose publiquement une SortedList(of key, value), cette liste est le résultat d'opération qui s'exécute à la fois dans une classe fille et parente.
    De l’extérieur de la classe je ne veux pas avoir accès aux méthode Add, Remove et tous ce qui pourrait modifier la liste.

    Je me dit que j'ai le choix entre :
    - Faire une classe qui Herite de SortedList et masquer les membres dont je ne veux pas avoir accès de l’extérieur de la classe. (Pourquoi pas, mais il ne faux pas oublier de masquer une propriété de la classe de base).

    - Ou faire ma propre SortedList(Of Key , value) basé sur une iCollection (je crois ). Par contre je n'ai pas vraiment d'exemple qui pourrait m'aider à faire une telle classe à part ce petit bout de code que j'ai trouvé et qui ressemble à ce que je voudrais mais ce n'est pas une collection du type sortedList je pense.

    Pouvez vous m'aider à faire ce type de classe ? (Les méthodes que j'utilise le plus sont : Add, Remove, Containskey et une énumération des keyValuePair et bien sur l'accés aux objets à partir de la clé).
    J'en demande beaucoup peut-être mais ça pourrait vraiment me rendre de gros service.......
    A propos : c'est quoi une ReadOnlyCollectionBase ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
     ''' <summary>
        ''' Represents the base collection.
        ''' </summary>
        ''' <typeparam name="T">The type of element.</typeparam>
        Public MustInherit Class BaseCollection(Of T)
            Inherits ReadOnlyCollectionBase
     
            '//properties
            ''' <summary>
            ''' Gets the element at the specified index.
            ''' </summary>
            ''' <param name="index">The index of the element to retrieve.</param>
            Default Public Overridable Overloads ReadOnly Property Item(ByVal index As Integer) As T
                Get
                    Return CType(MyBase.InnerList.Item(index), T)
                End Get
            End Property
     
            Private _graph As Graph
            ''' <summary>
            ''' Gets the Graph that contains the current instance.
            ''' </summary>
            Public ReadOnly Property Graph() As Graph
                Get
                    Return Me._graph
                End Get
            End Property
     
            '//constructors
            Friend Sub New(ByVal graph As Graph)
                If graph Is Nothing Then
                    Throw New ArgumentNullException("graph")
                End If
                Me._graph = graph
            End Sub
     
            '//methods
            Friend Overridable Sub Add(ByVal element As T)
                MyBase.InnerList.Add(element)
                Me.Graph.NotifyRecalculate()
            End Sub
     
            Friend Overridable Sub Remove(ByVal element As T)
                MyBase.InnerList.Remove(element)
                Me.Graph.NotifyRecalculate()
            End Sub
     
            Friend Overridable Sub Clear()
                MyBase.InnerList.Clear()
            End Sub
     
            ''' <summary>
            ''' Gets a value that indicates whether the specified element exists in the collection.
            ''' </summary>
            ''' <param name="element">The element to look for.</param>
            Public Overridable Function Contains(ByVal element As T) As Boolean
                Return MyBase.InnerList.Contains(element)
            End Function
     
            ''' <summary>
            ''' Retrieves the zero-based index of the specified element.
            ''' </summary>
            ''' <param name="element">The element to get the index for.</param>
            Public Overridable Function IndexOf(ByVal element As T) As Integer
                Return MyBase.InnerList.IndexOf(element)
            End Function
     
        End Class
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
     ''' <summary>
        ''' Represents a colleciton of Vertex objects.
        ''' </summary>
        Public Class VertexCollection
            Inherits BaseCollection(Of Vertex)
     
            '//properties
            ''' <summary>
            ''' Gets the first Vertex in the collection with the specified key.
            ''' </summary>
            ''' <param name="key">The key of the vertex to retrieve.</param>
            Default Public Overloads ReadOnly Property Item(ByVal key As String) As Vertex
                Get
                    For Each vertex As Vertex In Me
                        If vertex.Key.Equals(key) Then
                            Return vertex
                        End If
                    Next
                    Return Nothing
                End Get
            End Property
     
            ''' <summary>
            ''' Gets a value indicating whether this collection's verticies are all visited.
            ''' </summary>
            Public ReadOnly Property Finished() As Boolean
                Get
                    For Each vertex As Vertex In Me
                        If Not vertex.Visited Then
                            Return False
                        End If
                    Next
                    Return True
                End Get
            End Property
     
            '//constructors
            Friend Sub New(ByVal graph As Graph)
                MyBase.New(graph)
            End Sub
     
            '//methods
            ''' <summary>
            ''' Gets a value that indicates whether an element with the specified key exists in the collection.
            ''' </summary>
            ''' <param name="key">The key to search for.</param>
            Public Function ContainsKey(ByVal key As String) As Boolean
                Return Me(key) IsNot Nothing
            End Function
     
            ''' <summary>
            ''' Gets thezero-based index of the first element with the specified key.
            ''' </summary>
            ''' <param name="key">The key to search for.</param>
            Public Overloads Function IndexOfKey(ByVal key As String) As Integer
                Dim vertex = Me.Item(key)
                If vertex IsNot Nothing Then
                    Return MyBase.IndexOf(vertex)
                End If
                Return -1
            End Function
     
        End Class

  2. #2
    Membre chevronné
    Avatar de Sehnsucht
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Octobre 2008
    Messages
    847
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Lot et Garonne (Aquitaine)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Octobre 2008
    Messages : 847
    Points : 2 209
    Points
    2 209
    Par défaut
    Sinon tu pourrais ne pas exposer ta SortedList à l'extérieur (qui ne resterait donc qu'un détail d'implémentation privé à la classe) et exposer autre chose à celui qui utilise la classe (IEnumerable, IReadOnlyDictionary ou autre [potentiellement une classe custom] tout dépend des fonctionnalités souhaitées être exposées)
    Nous sommes tous plus ou moins geek : ce qui est inutile nous est parfaitement indispensable ( © Celira )
    À quelle heure dormez-vous ?
    Censément, quelqu'un de sensé est censé s'exprimer sensément.

  3. #3
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour

    Tu peux l'emballer dans un class Wrapper ...
    La liste initiale est passée en parametre comme un SortedList(of key ,of value) & et sera stockée dans une variable innerList...
    La List est exposé dans une prop ReadOnly qui recopie innerList du Wrapper...
    code .vb du Wrapper:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    Public Class Wraplist
        Private innerList As SortedList(Of String, String) = Nothing
     
        Public Sub New()
        End Sub
        Public Sub New(ByVal plist As SortedList(Of String, String))
            Me.New()
            innerList = plist
     
        End Sub
        Public ReadOnly Property MyList() As SortedList
            Get
                Dim s As New SortedList
                For Each item As KeyValuePair(Of String, String) In innerList
                    s.Add(item.Key, item.Value)
                Next
                Return s
            End Get
     
        End Property
     
    End Class
    code .vb du Form user:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
     
    Public Class Form1
        Dim wrap As New Wraplist
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim openWith As New SortedList(Of String, String)
     
            ' Add some elements to the list. There are no 
            ' duplicate keys, but some of the values are duplicates.
            openWith.Add("txt", "notepad.exe")
            openWith.Add("bmp", "paint.exe")
            openWith.Add("dib", "paint.exe")
            openWith.Add("rtf", "wordpad.exe")
     
     
     
            wrap = New Wraplist(openWith)
            ListBox1.DataSource = wrap.MyList.Values
            ListBox2.DataSource = wrap.MyList.Keys
        End Sub
     
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If wrap Is Nothing Then Return
            'aucun effet
            wrap.MyList.Add("tati", "bobo.exe")
            ListBox1.DataSource = wrap.MyList.Values
            ListBox2.DataSource = wrap.MyList.Keys
        End Sub
     
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If wrap Is Nothing Then Return
            wrap.MyList.Remove("txt")
            ListBox1.DataSource = wrap.MyList.Values
            ListBox2.DataSource = wrap.MyList.Keys
        End Sub
     
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            If wrap Is Nothing Then Return
            wrap.MyList.Clear()
        End Sub
    End Class
    c'est une liste dur comme un "écrin de diamant"
    bon code...

  4. #4
    Membre averti
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mars 2012
    Messages
    640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Bâtiment

    Informations forums :
    Inscription : Mars 2012
    Messages : 640
    Points : 372
    Points
    372
    Par défaut
    Bonjour,
    Merci de m'avoir donné quelques pistes. Il faut que j'étudie tous ça à tête reposé.
    Par ailleurs, j'ai aussi trouvé un document très intéressant :
    http://gilles.tourreau.fr/dotnet_fra..._0-et-4_5.html

  5. #5
    Membre averti
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mars 2012
    Messages
    640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Bâtiment

    Informations forums :
    Inscription : Mars 2012
    Messages : 640
    Points : 372
    Points
    372
    Par défaut
    Magnifique, je crois que j'ai trouvé une bonne solution

    Ça fonctionne à merveille.....ou plutôt comme je veux on va dire

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    Public Class MaClasse
    Private _MaSortedList As New SortedList(Of String, MonObjet)
    Public ReadOnly Property ReadOnlySortedList() As IReadOnlyDictionary(Of String, MonObjet)
            Get
                Return CType(_MaSortedList, IReadOnlyDictionary(Of String, MonObjet))
            End Get
    End Property
    End Class

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 0
    Dernier message: 15/06/2010, 16h43
  2. Réponses: 8
    Dernier message: 11/04/2010, 13h10
  3. Réponses: 4
    Dernier message: 05/02/2009, 11h45
  4. Lecture/écriture dans une mémoire protégée :S en C#
    Par ArnaudDev dans le forum Windows
    Réponses: 7
    Dernier message: 09/06/2008, 15h24
  5. Réponses: 4
    Dernier message: 02/03/2006, 16h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo