Slightly OT: Anyway to get compiler to catch this goof?

Slightly OT: Anyway to get compiler to catch this goof?

Old forum URL: forums.lhotka.net/forums/t/5521.aspx


rsbaker0 posted on Friday, October 03, 2008

So, yesterday I was implementing a new BusinessBase<T> derived class.

Through an oversight on my part, here is what happened.

I already had: (substituting class names for discussion)

class FirstClass : BusinessBase<FirstClass> ...

Then I went to implement SecondClass, and incorrectly declared it...

class SecondClass : BusinessBase<FirstClass> ...

This seemed to satisfy the generic constraint requirements and was not caught until runtime when I tried to Save the object and got an invalid cast exception because you can't cast a FirstClass to a SecondClass.

I actually have a common MyBusinessBase<T> class in between my classes and BusinessBase<T>. Is there something I could put in the declaration or implementation that would catch errors like this?

JoeFallon1 replied on Friday, October 03, 2008

It isn't always an error. In fact I have an inheritance hierarchy setup that way.

The "problem" is that FirstClass is probably fetching and saving items as FirstClass.

In my hierarchy, since I know I will be deriving SecondClass from FirstClass, the FirstClass code fetches and saves items as type SecondClass.

e.g.
FirstClass=MyAccountGen

SecondClass=MyAccount

All the code generated stuff in MyAccountGen like Fetch and Save refer to MyAccount.

Joe

 

 

rsbaker0 replied on Friday, October 03, 2008

I see. But is your declaration:

class SecondClass : FirstClass

or

class SecondClass : BusinessBase<FirstClass>

The first declaration would seem to define an "is-a" relationship (e.g. a SecondClass is-a FirstClass), while I don't think this true of the latter case.

JoeFallon1 replied on Friday, October 03, 2008

class SecondClass : FirstClass

Copyright (c) Marimer LLC