Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making abstract classes invisible; or: hiding my BananaHuman

Background: I am still a C# novice and this is my first big project with inheritance. The following story is a simplified example of my current situation:

Suppose I have a class called LivingOrganism. Every living creature can use this class as base and inherit the functionality that is common to all living organisms.

When I was working on some of these derived classes, I found out that bananas and humans are very similar. Although it doesn't make much sense and they look nothing alike, they apparently have most of their "functionality" in common.

Duplication in code is bad, so I wrote a new class to reduce maintenance costs. This class is called: BananaHuman. My Human class and Banana class inherit from BananaHuman.


Problem:

I have no problem with my BananaHuman (i.e. I understand what it means and why it exists). But eventually, other people (even those who don't (fully) understand inheritance) will have to use my LivingCreatures.dll. And they won't understand why intellisense suggests BananaHuman when they type 'B'.

And consider the following piece of code:

//Valid and makes sense.
foreach(LivingOrganism foo in cityOfNeyYork) { /*embedded statement*/ }

But imagine how weird/confusing it would look if we substitute Living Organism with BananaHuman.

I can't make BananaHuman private, protected or protected internal (Elements defined in a namespace cannot be explicitly declared that way). I also can't make it internal, because Human and Banana have to be public. If I try this, I get an error message saying there is an inconsistent accessibility issue.

I feel like I am missing the obvious, but what can/should I do? I am left with a couple of options/questions:

  1. Is it possible to "hide" BananaHuman to avoid confusion?
  2. Should I rewrite BananaHuman to something very long and technical such as DnaRelatedOrganismsType[X], where "X" describes their unique relation?
  3. Should I just delete BananaHuman, let Human and Banana inherit from LivingOrganism and do the extra maintenance when something needs changing?
  4. Is there another solution I am completely missing?

I searched around and couldn't quite find a "fixed pattern" for this situation. I found this question with a similar title, but I don't know if the answers are applicable because it appears that he is asking something completely different.

Any help is greatly appreciated!

like image 967
Jordy Avatar asked Aug 21 '13 08:08

Jordy


1 Answers

You can use the EditorBrowsableAttribute and apply it to your class. This will make you class disappear from Intellisense if people are using your .dll. If you have your project referenced instead of the dll it will still be visible.

Use like:

[EditorBrowsable(EditorBrowsableState.Never)]
public class BananaHuman
{
    //....
}

So if you would give me your .dll I wouldn't see BananaHuman pop up in Intellisense. But if I would inspect the Banana or Human class I would still see it inherited from BananaHuman because that IS the case. The EditorBrowsable attribute just makes it disappear from Intellisense, what is what you want.

like image 67
SynerCoder Avatar answered Oct 24 '22 22:10

SynerCoder