Question

I have a class with many, many (not shared) methods. I'd like to organize them into subcategories so that for example, instead of calling

(my class instance).BuildingComponentDoorCreate

I could call

(my class instance).Building.Component.Door.Create

since there are other components besides a door and door specific stuff has other methods than create and so on.

I'm building the class as a sort of framework and right now it just feels very crowded to use. Also, I'd rather not create separate classes for "building" and the components inside the building since it would just add unnecessary complications.

Was it helpful?

Solution

Given the exact wording of your question, the answer is "no."

If you have a variable x that is an instance of a class, and you want to write x.<something> in your code, the <something> has to be a property or public field.

If you can allow for brackets then you could also have methods, e.g. x.Something(). This could be a method declared on the class itself, or an extension method from another static class.

Under no circumstances can the .<something> following your class instance be a namespace.

You say that your class is crowded; that suggests that you should consider creating more classes to better separate your functionality. It's also the only way that you'll be able to organise them in the way you describe.

OTHER TIPS

Use inner classes, which is class inside the existing class.

I have found unless you already have your infrastructure organized in this manner and a useful need to separate into components it can be more work then it is worth if you already have everything inside a master class.

To go 4 levels deep is also a very large separation of items but the subclassing will inevitably make your code more readable and organized. so it really is a judgment call on your part if you are going to do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top