Inheritance question

  • Thread starter Thread starter 1388-2/HB
  • Start date Start date
1

1388-2/HB

Public Class IAmADataRow
Inherits System.Data.DataRow

Public Sub StandOnYourHead()
'this code has to be the same as
IDoNotNeedToBeADataRow.StandOnYourHead
End Sub

End Class

Public Class IDoNotNeedToBeADataRow

Public Sub StandOnYourHead()
'this code has to be the same as IAmADataRow.StandOnYourHead
End Sub

End Class


My question is, in order to avoid the maintenance issue of maintaining
"StandOnYourHead" in two places, am I essentially forced to inherit from
DataRow in the "IDoNotNeedToBeADataRow" Class?
 
Hi,

Yes basically you'd need to put the method in a common base class, which
would effectively mean it would derive from DataRow.

In .NET the only implementation inheritance is single chain... 1:1:1
Interface inheritance on the other hand is multi-branched. Technically
Interface definitions can actually include concrete implementation for
Shared (static) methods but neither VB or C# support that.

If the single chain inheritance doesn't work, I'd be looking at interfaces,
and seeing if I could move the common code into a factory or Shared utility
method in a Shared (static) class. In VB9, you could also use Extension
methods.
http://www.ftponline.com/vsm/2007_05/magazine/columns/programmingtechniques/

Regards,

Bill.
 
No, create another level in your inheritance tree.

----------------------------

Public Class myBaseDataRow
Inherits System.Data.DataRow

Public Sub StandOnMyHead
...
End Sub

End Class

----------------------------

Public Class IAmADataRow
Inherits myApp.myNamespace.myBaseDataRow

Public Sub GetMyData
... sub specific to the IAmADataRow class.
End Sub

End Class

----------------------------

Public Class IAmNotADataRow
Inherits myApp.myNameSpace.myBaseDataRow

Public Sub GetWhatIGot
... sub specific to the IAmNotADataRow class.
End Sub

End Class

----------------------------

You maintain 1 instance of your StandOnMyHead code ... now if you want to
be able to 'override' or have a different code block for StandOnMyHead, you
can make it overridable... This way if you have a descendent that after
StandOnMyHead their money changes falls out of their pockets, you can extend
the base sub by using the overrides.

My suggestion is whenever you are creating an inheritance tree you should
create at least one level between the system tree and your descendants.
That way, if you need to code something generic for all descendents, you can
slip it in your own object. You can create branches in your inheritance
structure to meet other needs. Inheritence is a powerful tool that can
save a lot of coding ... but be careful ... you can create a maintenance
nightmare if you start creating many branches along the way.

This is how I would tackle this using inheritance, if the StandOnMyHead
truly is the same bit of code for each descendant class.

Interfaces would require to maintain seperate code blocks for each class
that implements myBaseDataRowInterface. As the interface ensures the class
has the code block ... you still need to code each one seperately. Please
correst me if I am wrong.

Thanks
Jeff
 
Well the truth is a single chain works fine (jeff's single chain suggestion
in this thread was in fact the very catalyst of my original question and
I'll probably keep doing it that way). It's a philosophical thing I guess,
particularly after reading advice to the effect of "if single-chain
inheritance doesn't work for you, you're structuring your classes wrong". I
can generally agree with that, but I also have to give a nod to the
"normalization" power of multi class inheritance, because this would sure be
handy right about now:

Mustinherit Class StandOnYourHead
End Class

Class DataRowish
Inherits DataRow, StandOnYourHead
End Class

Class NotAtAllDataRowish
Inherits StandOnYourHead
End Class

Oh well. At one point it occurred to me that if I moved the "stand on your
head" stuff to an external class or module, I could at least have it exist
physically in one place for the sake of maintenance and the only
"cut-n-paste" in my classes would be a single method call to that external
class or module. But I wasn't really sure how best to implement that...
I'll read up on the shared / static classes and see where that takes me.

Thanks
 
Back
Top