• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

Defining a class parent (not inheritance)

Hi, I am new to VB.NET but an expert at VB6. I just wanted to check if I am still thinking in the correct way for defining a class instances parent/owner.

To further explain.
I do not mean inheritance, e.g. an Apple class inherits the properties of a Fruit class.

I mean I have a collection of Apples and for each apple in that collection I want it to know which Apples collection is its owner or parent.

Am I right in thinking you would still express this like so

Public Class Apples

    public function Add() As Apple
        'Code to add apple into collection
        '...

        return new Apple(Parent:=me)
    end function

End Class

Public Class Apple
    private m_MyParent As Apples

    public readonly property Parent() As Apples
        Get
            return m_MyParent
        End Get
    end property

    public sub New(byval Parent As Apples)
        m_MyParent = Parent
    end sub
End Class
0
joboy
Asked:
joboy
  • 6
  • 4
1 Solution
 
S-TwilleyCommented:
Looks fine to be... but just as a matter of preference... Id add a few more version for your Add method... so you can add an existing apple to the collection:

Public Class Apples

    public function Add() As Apple
        'Code to add apple into collection
        '...

        return new Apple(Me)
    end function

    public function Add(ByVal newApple As Apple) As Apple
        'Code to add newApple into collection
        '...
        newApple.Parent = Me
        return newApple
    end function
End Class

==========

and change Parent to "normal" instead of readonly... but thats a matter of preference... so it emulates other collections like control collections
e.g.

Dim listitem1 as ListViewItem = ListView1.Items.Add("Test Text")

or

Dim listitem1 as New ListViewItem
listitem1.Text = "Test Text"
ListView1.Items.Add(listitem1)
0
 
joboyAuthor Commented:
Thanks for the response...

Just wanted to check that's it is still the standard practice for letting objects know who owns them.

The code is purely an example (off the top of my head!).  Thus in real world parameters and properties would be different.  That said, wouldn't the Parent property (nearly) always be exposed as read only as you wouldn't normally want anyone else changing an object parent.  That's why I made it a required property of the New constructor.

0
 
S-TwilleyCommented:
Well I tried to give you an example of a child item... which is created without a parent (poor thing)... but is then added to a collection, thus its parent is assigned.  You don't have to implement such an idea... and if you don't, then you need to remove the read only property of the parent.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
S-TwilleyCommented:
Let me think about this, should be able to keep the "Parent" property as readonly, but also have the possibility of assigning an already created child to the collection and set its parent. Back soon!
0
 
S-TwilleyCommented:
Well no luck yet... I was trying to look for a way for the Apples class to have permission on the m_MyParent of class Apple... without other classes having access... never had to use anything like that before.

Anyway... your approach was fine to start with, I was just being picky and wanted to add to it :P
0
 
joboyAuthor Commented:
OK,
Thanks for looking into this for me....

You've answered the question, which was just to confirm I was still doing things right in .NET.

Thanks again
Joboy
0
 
joboyAuthor Commented:
I see what your trying to do.

Presumably you can do something like the following?

Public Class Apples

    public function Add() As Apple
        'Code to add apple into collection
        '...

        return new Apple(Me)
    end function

    public function Add(ByVal newApple As Apple) As Apple
        'Code to add newApple into collection
        '...
        newApple.OwnerObject = Me
        return newApple
    end function
End Class

Public Interface IOwnerContract
    WriteOnly Property OwnerObject() As Object
End Interface

Public Class Apple
    Implements IOwnerContract

    Private m_MyParent As Apples
    Friend WriteOnly Property OwnerObject() As Object Implements IOwnerContract.OwnerObject
        Set(ByVal Value As Object)
            m_MyParent = DirectCast(Value, Apples)
        End Set
    End Property

    Public Sub New()
        'Do whatever
    End Sub

    Public Sub New(byval Parent As Apples)
        m_MyParent=Parent
    End Sub

End Class



Dim GoldenDelicious As New Apple()

Orchard.Add(GoldenDelicious) 'Golden delicious apple would now be made a child of the Orchard! But only classes within the project would be able to change the Apple's owner/parent!
0
 
S-TwilleyCommented:
what's that iownercontract?

can't find it in my help files... but that looks like what i was trying to do...

i probably needed to create the classes in a seperate project. to the windows form project.. which is why the friend keyword wasnt working.

you'd want to add a readonly parent property though...

and also:

Private m_MyParent As Apples = Nothing

good practice to give it some sort of value... even if its nothing
0
 
joboyAuthor Commented:
"IOwnerContract" is an interface definition that is then implemented by the Apples class.  This is only done so there is a standard interface to interact with (a contract if you will).  Thus if I was to add Pear, Peach and Orange classes I could implement the IOwnerContract interface in each class.  This would guarantee me that the OwnerObject property would always exist and be defined in the same way.  It's not 100% necessary in this case.

>i probably needed to create the classes in a seperate project. to the windows form project.. which is why the friend keyword wasnt working.
Yeh!, I did that at first and kept thinking why CAN I see these properties :)

?you'd want to add a readonly parent property though...
Yep, you're right - missed that!

Lastly thanks for the final tip.  It's good to hear little tips like that!!  That's what I'm mainly looking for as I learn VB.NET, the new conventions, best practices etc.
0
 
S-TwilleyCommented:
sorry... i know what an interface is... just looked like it was a pre-defined one.  :P
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now