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
Partager