0

I'm quite new to OO programming. I'm building a class in VB.NET and I want properties to be grouped within certain categories. Should the categories be declared as a Subclass or as a Property? In other words, which of the two approaches is better?

Class Tree
    Property Trunk As Trunk
    Property Leaves As Leaves
End Class

Class Trunk
    Property Color As String
    Property Diameter As Integer
End Class

Class Leaves
    Property Color As String
    Property Width As Integer
    Property Height As Integer
End Class

or

Class Tree
    Class Trunk
        Property Color As String
        Property Diameter As Integer
    End Class
    Class Leaves
        Property Color As String
        Property Width As Integer
        Property Height As Integer
    End Class
End Class

In both cases, I will use the instance of Tree with something like myTree.Trunk.Color = "Red", but which one is considered as a best practice?

Thank you for your help.

Emanuele
  • 1
  • 1
  • 1
    I'm not sure either way is definitively "best"...it depends almost entirely on the use case. If put a class inside another class, it's often because you only ever use that class within the outer class (i.e. no outside code ever references it) or because it only has any meaning within the context of the the code of the outer class (but you make it public so other code can still interact with it if necessary) – ADyson Nov 08 '18 at 10:46
  • Possible duplicate of https://stackoverflow.com/questions/48872/why-when-should-you-use-nested-classes-in-net-or-shouldnt-you – Rno Nov 08 '18 at 10:47
  • 2
    P.S. Bear in mind that even if you put the Trunk class inside your Tree class, you would still need a _property_ called Trunk (which is of type Trunk) in order to write the `myTree.Trunk.Color` example you gave above. From a language point of view (leaving aside the design considerations), moving the location of the class only affects its ability to be accessed from elsewhere, and the way that you instantiate it. – ADyson Nov 08 '18 at 10:50
  • 3
    You might do both. You need to declare properties in the `Tree` class as you did in the first example regardless. If the other classes are never going to be used independently then you might nest them as well. Just keep in mind that you can't have a nested class and a property with the same name. – jmcilhinney Nov 08 '18 at 11:10

1 Answers1

0

That depends on the re-usability you want to have. In your example the first approach allows you to use Trunk and Leaves classes for another class, for example, a flower.

Class Flower
    Property Trunk As Trunk
    Property Leaves As Leaves
End Class

Class Tree
    Property Trunk As Trunk
    Property Leaves As Leaves
End Class

Of course you can re-use the inner classes (your second example) however you may have to adjust the scopes and in large projects that may complicate things, anyway if you follow your second approach then the class Flower should be declared as follows:

Class Flower
    Property Trunk As Tree.Trunk
    Property Leaves As Tree.Leaves
End Class

You can see the difference.

Another thing that you should have in mind is inheritance. In your example you can have a plant class declared as follows:

Class Plant
    Class Trunk
        Property Color As String
        Property Diameter As Integer
    End Class
    Class Leaves
        Property Color As String
        Property Width As Integer
        Property Height As Integer
    End Class

    Property Trunk As Trunk
    Property Leaves As Leaves
End Class

And then you can inherit from it in another class or classes:

Class Tree
    Inherits Plant
    'extra tree properties here
End Class

Class Flower
    Inherits Plant
    'extra flower properties here
End Class

That way both Tree and Flower classes will have the properties Trunk and Leaves:

Tree.Trunk.Color
'...
Flower.Leaves.Width

Hope this helps!

ChD Computers
  • 3,135
  • 3
  • 23
  • 33