Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Reference to parent collection

Reference to parent collection

Scheduled Pinned Locked Moved Visual Basic
wpfquestioncsharphelplounge
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ausadmin
    wrote on last edited by
    #1

    Apologies in advance, I suspect there may be an obvious answer to this, but I have searched these forums and google and not really been able to determine the solution. This is for a WPF application, but I think it is a more general programming question. We have an ObservableCollection(of T), where we need do do some validation of the member object, T. One of the rules is that a property of T (say name) cannot already exist in the collection. Our validation class is instantiated from the xaml, so I don't know of any way to pass in the reference to the ObservableCollection. However, this is needed to be able to step through the collection, to see if name has already been used, and report an error (if needed). One solution I can see is that the class T could have a property which is an ObservableCollection of T, and then you pass the parent collection into the member object. I think this will solve the issues, but something says to me this would be bad practise... Can anyone clarify whether this will cause errors or should be avoided? Can anyone suggest an alternative? More specifically, say we define Person

    Public Class Person
    Public Sub New(ByVal Name As String, ByVal People As ObservableCollection(Of Person))
    ...
    End Sub

    Public Property Name() As String
        ...
    End Property
    
    Public Property People() As ObservableCollection(Of Person)
        ...
    End Property
    

    End Class

    Then elsewhere make a collection of Person

        Dim MemberList As New ObservableCollection(Of Person)
        MemberList.Add(New Person("John", MemberList))
        MemberList.Add(New Person("Fred", MemberList))
        MemberList.Add(New Person("Jane", MemberList))
        MemberList.Add(New Person("Andrew", MemberList))
    

    Then we can bind say a WPF DataGrid to MemberList, and use validation to check our rule, to check that the Person name has not already been used in the ObservableCollection(of Person) - ie MemberList. The approach here will work, but I am concerned that there might be underlying problems - some circular reference or something (or else just not good practise). Thanks for any suggestions. Tim

    modified on Tuesday, October 27, 2009 1:40 AM

    D 1 Reply Last reply
    0
    • A ausadmin

      Apologies in advance, I suspect there may be an obvious answer to this, but I have searched these forums and google and not really been able to determine the solution. This is for a WPF application, but I think it is a more general programming question. We have an ObservableCollection(of T), where we need do do some validation of the member object, T. One of the rules is that a property of T (say name) cannot already exist in the collection. Our validation class is instantiated from the xaml, so I don't know of any way to pass in the reference to the ObservableCollection. However, this is needed to be able to step through the collection, to see if name has already been used, and report an error (if needed). One solution I can see is that the class T could have a property which is an ObservableCollection of T, and then you pass the parent collection into the member object. I think this will solve the issues, but something says to me this would be bad practise... Can anyone clarify whether this will cause errors or should be avoided? Can anyone suggest an alternative? More specifically, say we define Person

      Public Class Person
      Public Sub New(ByVal Name As String, ByVal People As ObservableCollection(Of Person))
      ...
      End Sub

      Public Property Name() As String
          ...
      End Property
      
      Public Property People() As ObservableCollection(Of Person)
          ...
      End Property
      

      End Class

      Then elsewhere make a collection of Person

          Dim MemberList As New ObservableCollection(Of Person)
          MemberList.Add(New Person("John", MemberList))
          MemberList.Add(New Person("Fred", MemberList))
          MemberList.Add(New Person("Jane", MemberList))
          MemberList.Add(New Person("Andrew", MemberList))
      

      Then we can bind say a WPF DataGrid to MemberList, and use validation to check our rule, to check that the Person name has not already been used in the ObservableCollection(of Person) - ie MemberList. The approach here will work, but I am concerned that there might be underlying problems - some circular reference or something (or else just not good practise). Thanks for any suggestions. Tim

      modified on Tuesday, October 27, 2009 1:40 AM

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Wait a second. Let me see if I understand you. You're trying to have a member object search it's parent collection to see if another object in the collection has a property value that already exists?? Correct?? This is backwards. Under OOP concpets, a member object should never even know that it is part of a collection, let alone how to manipulate it's parent collection. I think a better way would be to create a PeopleCollection inheriting from ObservableCollection and overriding the Add method to check for this.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008
      But no longer in 2009...

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Wait a second. Let me see if I understand you. You're trying to have a member object search it's parent collection to see if another object in the collection has a property value that already exists?? Correct?? This is backwards. Under OOP concpets, a member object should never even know that it is part of a collection, let alone how to manipulate it's parent collection. I think a better way would be to create a PeopleCollection inheriting from ObservableCollection and overriding the Add method to check for this.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008
        But no longer in 2009...

        A Offline
        A Offline
        ausadmin
        wrote on last edited by
        #3

        Dave, Thank you very much for your reply and for setting me in the right direction! Yes, you understood correctly - and your response crystalises the concerns I had about doing this. Thanks again. In fact the snippet posted was overly simplified - we already inherit from ObservableCollection and implement some Find and Sort methods, so we could override the Add method. What I am concerned about is a user editing the value to make it invalid, then being able to catch this scenario and alert the user. I know of many ways to do this, but I am trying to determine the best approach in WPF - so I will take this question to that forum (and in fact I have had the specific question on a relevant article for a few weeks but no reply). Your help is appreciated. Regards, Tim

        D 1 Reply Last reply
        0
        • A ausadmin

          Dave, Thank you very much for your reply and for setting me in the right direction! Yes, you understood correctly - and your response crystalises the concerns I had about doing this. Thanks again. In fact the snippet posted was overly simplified - we already inherit from ObservableCollection and implement some Find and Sort methods, so we could override the Add method. What I am concerned about is a user editing the value to make it invalid, then being able to catch this scenario and alert the user. I know of many ways to do this, but I am trying to determine the best approach in WPF - so I will take this question to that forum (and in fact I have had the specific question on a relevant article for a few weeks but no reply). Your help is appreciated. Regards, Tim

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Hmmmm.... I think you might have to change from inheriting from ObservableCollection to implementing your own collection class. You'd probably inherit from Collection(Of T) and implement the two interfaces INotifyCollectionChanged and INotifyPropertyChanged. You'd keep a collection internal inside the class and explose it just like ObservableCollection does, but add code in the Items(Of T) property to check for this condition.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008
          But no longer in 2009...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups