Skip to main content

C# How to combine types I don't have access to? [Resolved]

I have been developing a library with the help of another library.

So what I have set up here is two classes sub-classing the library's classes for additional functionality. The problem comes in when I want to be able to use the classes interchangeably in another class. how sad am i So I want to be able to pass these graphics into my object through the constructor as long as it is of type "super class". The problem is, Rectangle and Circle have different property names for setting their position and in my object I need to set their position. I could check the type then cast, but then if someone wants to pass in a graphic like triangle, they'd have to edit the class to check for type triangle.

What I chose to do instead was create an interface ICenterable that my circle and rectangle class implement that requires a CenterX and CenterY property. Now anyone who has an ICenterable object can pass it into the object. This would be perfect, except now I have to choose whether the parameter being passed will be types as ICenterable or "super class". Once again, I could cast when needing certain functionality, but what if, for example, a "super class" is passed in that doesn't implement ICenterable. In the constructor I could check to make sure it's of both types or throw an error but that seems sloppy.

The best solution would be if I could state that the object being passed must implement ICenterable and extend "super class". I would do this through making a class that does both, but then my circle and rectangle classes wouldn't be able to extend that class because they need to extend Circle and Rectangle. enter image description here It seems like the more I do the worse it gets. Should I just accept it's not going to be how I want, forget the interface, and just check whether it's a rectangle, circle, and add more later if needed? Or is there a better way of doing it?

Thanks


Question Credit: TheJesBoy
Question Reference
Asked September 17, 2019
Posted Under: Programming
24 views
2 Answers

If I am getting this right, then your posted case is only a small part of the problem you are facing. If Circle and Rectangle have different methods for setting the position, then what is it that the Superclass is abstracting away (which, I guess, is something along the lines of Shape)?

If, for your specific use case, Circle and Rectangle are already different in terms of implementation, then the Superclass type is not really helping you (i.e. it does not define the useful single property/method you desire from all implementations). You should consider redefining the type and potentially wrapping those external-library types (shapes) as extensions of your own type, as John Wu advised. In this way, you can define your own uniform set of properties/methods that you need all shapes to have.


credit: Vector Zita
Answered September 17, 2019
Your Answer