none
Collections RRS feed

  • Frage

  • Hallo Leute,
    wie muss die Function 'RemoveItem' aussehen, dass diese die Collection(of cParent) und die Collection(of cChild) verarbeiten kann.
    Mit colParent funktioniert es.

    Module Module1
    
        Sub Main()
            Dim colParent As New Collection(Of cParent)
            Dim Parent As New cParent
    
            Dim colChild As New Collection(Of cChild)
            Dim Child As New cChild
    
            colParent.Add(Parent)         'Parent testweise hinzufügen
            RemoveItem(colParent, Parent) 'Parent wieder entfernen
    
            colChild.Add(Child)           'Child testweise hinzufügen
            RemoveItem(colChild, Child)   '>>>>Übergabe funktioniert nicht<<<<
    
        End Sub
    
        Private Sub RemoveItem(ByRef col As Collection(Of cParent), ByVal Parent As cParent)
            col.Remove(Parent)
        End Sub
    
        'Parent
        Private Class cParent
        End Class
    
        'Child
        Private Class cChild
            Inherits cParent
        End Class
    
    End Module




    Donnerstag, 30. November 2017 14:39

Antworten

  • Hallo Christian,

    ich hab zwar nicht wirklich verstanden, was Du meinst aber hier dennoch mal ein Versuch.

    Ist allerdings grade so aus der Hüfte geschossen und nicht verifiziert und schon gar nicht getestet :) Könnte also auch fehlerhaft sein.

    ---

    Du kannst entweder beiden Klassen ein Interface verpassen und die Methode dann auf den Interfacetyp umstellen oder generisch arbeiten.

    Module Module1
    
        Sub Main()
            Dim colParent As New Collection(Of cParent)
            Dim Parent As New cParent
    
            Dim colChild As New Collection(Of cChild)
            Dim Child As New cChild
    
            colParent.Add(Parent)         'Parent testweise hinzufügen
            RemoveItem(colParent, Parent) 'Parent wieder entfernen
    
            colChild.Add(Child)           'Child testweise hinzufügen
            RemoveItem(colChild, Child)   '>>>>Übergabe funktioniert nicht<<<<
    
        End Sub
    
        Private Sub RemoveItem( ByVal col As Collection(Of IParentChild ), ByVal Parent As IParentChild )
            col.Remove( Parent )
        End Sub
    
        'Parent
        Private Class cParent
    
            Implements IParentChild
    
        End Class
    
        'Child
        Private Class cChild
    
            Inherits cParent
    
            Implements IParentChild
    
        End Class
    
        Private Interface IParentChild
    
        End Class
    
    End Module

    Bzw.

    Private Sub RemoveItem( Of T )( ByVal col As Collection( Of T ), ByVal Parent As T )
        col.Remove( Parent )
    End Sub

    Aufruf dann mit:

    RemoveItem( Of Parent )( colParent, Parent )
    RemoveItem( Of Child )( colChild, Child )

    BTW: ByRef und ByVal helfen dir hier nicht. Schau dir dazu bitte mal die akzeptierte Antwort in folgendem Thread an:

     ByVal and ByRef with reference type


    Gruß, Stefan
    Microsoft MVP - Visual Developer ASP/ASP.NET
    http://www.asp-solutions.de/ - Consulting, Development
    http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community

    Donnerstag, 30. November 2017 15:10
    Moderator
  • Hi Christian,
    hier mal eine Lösung, auf die schon Stefan hingewiesen hat.

    Imports System.Collections.ObjectModel
    
    Module Module1
    
      Sub Main()
    
        Dim colChild As New Collection(Of IParentChild)
        Dim Child As New cChild(colChild)           'geht nicht
        colChild.Add(Child)
    
      End Sub
    
      'Parent
      Private Class cParent
        Implements IParentChild
    
        Private _col As Collection(Of IParentChild)
    
        Public Sub New(ByVal col As Collection(Of IParentChild))
          _col = col
        End Sub
    
      End Class
    
      'Child
      Private Class cChild
        Inherits cParent
        Implements IParentChild
    
        Public Sub New(ByVal col As Collection(Of IParentChild))
          MyBase.New(col)
        End Sub
    
      End Class
    
    End Module
    
    Public Interface IParentChild
      ' Member, die sowohl cParent als auch cChild haben sollen
    End Interface
    


    --
    Viele Grüsse
    Peter Fleischer (ehem. MVP)
    Meine Homepage mit Tipps und Tricks

    Donnerstag, 30. November 2017 19:08

Alle Antworten

  • Hallo Christian,

    ich hab zwar nicht wirklich verstanden, was Du meinst aber hier dennoch mal ein Versuch.

    Ist allerdings grade so aus der Hüfte geschossen und nicht verifiziert und schon gar nicht getestet :) Könnte also auch fehlerhaft sein.

    ---

    Du kannst entweder beiden Klassen ein Interface verpassen und die Methode dann auf den Interfacetyp umstellen oder generisch arbeiten.

    Module Module1
    
        Sub Main()
            Dim colParent As New Collection(Of cParent)
            Dim Parent As New cParent
    
            Dim colChild As New Collection(Of cChild)
            Dim Child As New cChild
    
            colParent.Add(Parent)         'Parent testweise hinzufügen
            RemoveItem(colParent, Parent) 'Parent wieder entfernen
    
            colChild.Add(Child)           'Child testweise hinzufügen
            RemoveItem(colChild, Child)   '>>>>Übergabe funktioniert nicht<<<<
    
        End Sub
    
        Private Sub RemoveItem( ByVal col As Collection(Of IParentChild ), ByVal Parent As IParentChild )
            col.Remove( Parent )
        End Sub
    
        'Parent
        Private Class cParent
    
            Implements IParentChild
    
        End Class
    
        'Child
        Private Class cChild
    
            Inherits cParent
    
            Implements IParentChild
    
        End Class
    
        Private Interface IParentChild
    
        End Class
    
    End Module

    Bzw.

    Private Sub RemoveItem( Of T )( ByVal col As Collection( Of T ), ByVal Parent As T )
        col.Remove( Parent )
    End Sub

    Aufruf dann mit:

    RemoveItem( Of Parent )( colParent, Parent )
    RemoveItem( Of Child )( colChild, Child )

    BTW: ByRef und ByVal helfen dir hier nicht. Schau dir dazu bitte mal die akzeptierte Antwort in folgendem Thread an:

     ByVal and ByRef with reference type


    Gruß, Stefan
    Microsoft MVP - Visual Developer ASP/ASP.NET
    http://www.asp-solutions.de/ - Consulting, Development
    http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community

    Donnerstag, 30. November 2017 15:10
    Moderator
  • Hallo Stefan,
    danke für deine Antwort. Dein Code funktioniert.
    Jedoch habe ich meine Frage falsch gestellt.

    Der Hintergrund ist, dass ich in Child die Collection speichern möchte in der sich Child selbst befindet, damit Child
    einen Zugriff auf die übergeordnete Collection hat.
    Würde ich das Ganze mit der Klasse cParent machen, dann funktioniert es. Es ist für mich aber erforderlich, dass
    dies auch mit der von cParent abgeleiteten Klasse cChild funktioniert.

    Imports System.Collections.ObjectModel
    
    Module Module1
    
        Sub Main()
    
            Dim colChild As New Collection(Of cChild)
            Dim Child As New cChild(colChild)           'geht nicht
            colChild.Add(Child)
    
        End Sub
    
        'Parent
        Private Class cParent
            Private _col As Collection(Of cParent)
    
            Public Sub New(ByVal col As Collection(Of cParent))
                _col = col
            End Sub
    
        End Class
    
        'Child
        Private Class cChild
            Inherits cParent
    
            Public Sub New(ByVal col As Collection(Of cParent))
                MyBase.New(col)
            End Sub
    
        End Class
    
    End Module
    
    mfg


    Christian Tauschek

    Donnerstag, 30. November 2017 18:34
  • Hi Christian,
    hier mal eine Lösung, auf die schon Stefan hingewiesen hat.

    Imports System.Collections.ObjectModel
    
    Module Module1
    
      Sub Main()
    
        Dim colChild As New Collection(Of IParentChild)
        Dim Child As New cChild(colChild)           'geht nicht
        colChild.Add(Child)
    
      End Sub
    
      'Parent
      Private Class cParent
        Implements IParentChild
    
        Private _col As Collection(Of IParentChild)
    
        Public Sub New(ByVal col As Collection(Of IParentChild))
          _col = col
        End Sub
    
      End Class
    
      'Child
      Private Class cChild
        Inherits cParent
        Implements IParentChild
    
        Public Sub New(ByVal col As Collection(Of IParentChild))
          MyBase.New(col)
        End Sub
    
      End Class
    
    End Module
    
    Public Interface IParentChild
      ' Member, die sowohl cParent als auch cChild haben sollen
    End Interface
    


    --
    Viele Grüsse
    Peter Fleischer (ehem. MVP)
    Meine Homepage mit Tipps und Tricks

    Donnerstag, 30. November 2017 19:08