We help IT Professionals succeed at work.

C# - Extend, inherit, contain, or what?

gmayo
gmayo asked
on
Medium Priority
1,275 Views
Last Modified: 2013-11-07
I have a network of nodes and edges, each edge connecting to exactly two nodes, and each node having zero to many edges. That's my base data which several programs will load.

Next, one particular program will want to store screen coordinates with the nodes, and a series of Points for the edges, plus a line style to draw the edges in. I don't want this in the base data as it's not relevant to other programs, so it needs to be in seperate class of some kind. Another program might want to store 3d coordinates for the nodes rather than just 2d, and have superelevation on the edges. Again, this is superfluous data that I don't want in the base class.

As far as I can tell there are three main ways of doing so:
class LatLonNode { public BaseNode node } (containment)
class LatLonNode : BaseNode (inheritance)
class LatLonNode { public DoSomething(this BaseNode bn) {} } (extension)

Extension looks confusing and can it have properties? Containment looks messy. Inheritance, from past experience, would normally be the way to go but I'm trying to learn new techniques!

Any recommendations for/against any particular method would be appreciated. Thanks.
Comment
Watch Question

There is no fundamental difference between inheritance and extension. Say you want to add a new method to an exisiting class. Using inheritance you do it like  this:
public class BaseClass
{}

public NewClass:BaseClass
{
  public void NewMethod()
  {}
}

using extension you do it like this:
public static class MyExtensions
{
  public static void NewMethod(this BaseClass base)
  {}
}

using extension you can only add methods to a type; no fields, or properties.

see
http://msdn.microsoft.com/en-us/library/bb383977.aspx

for more details

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
I think, despite my earlier reservations, that containment fits the way I need to go. I should have said that there are other factors, namely RTTI, that doesn't work with inheritance or extension. But thanks anyway.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.