1

Ok, I have a c# project named BusinessLayer which produces an assembly called BusinessLayer and the namespace is BusinessLayer.

Inside of this project, I am using folders to store code. One folder is called FilterElements and it has folders called FilterKeyReversal, FilterRandom and FilterToday.

Let's take the example of the FilterRandom folder. It has a class called LessThan10DaysGreaterThan50A with a namespace of BusinessLayer.FilterElements.FilterRandom and a single public static method called RunFilter();

In the code behind page of the website that is consuming this method, I have the using statement, Using BusinessLayer. I also have another using statement, using BusinessLayer.FilterElements.

I would think that to expose the RunFilter() method of the LessThan10DaysGreaterThan50A class, I could use the following syntax: FilterRandom.LessThan10DaysGreaterThan50A.RunFilter(), however I get the following error: The name FilterRandom does not exist in the current context.

If I use the following syntax inline, the error goes away: BusinessLayer.FilterElements.FilterRandom.LessThan10DaysGreaterThan50A.RunFilter(), or if I use a using statement of: Using BusinessLayer.FilterElements.FilterRandom, the following syntax works: LessThan10DaysGreaterThan50A.RunFilter().

I would rather use FilterRandom.LessThan10DaysGreaterThan50A.RunFilter() as it seems to make code more readable. If I use an alias with the following syntax of using FilterRandom = BusinessLayer.FilterElements.FilterRandom, I can get what I want, but don't really like the idea of using an alias since it can lead to confusion down the line.

I thought that since my BusinessLayer namespace has nested namespaces, I'd be able to pick up the remaining namespace, but I can't seem to get it to work. Anybody know how to make this work without using an alias, or am I going to have to use the entire namespace name every time?

Thanks.

ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
Mike Malter
  • 1,018
  • 1
  • 14
  • 38
  • 2
    This works in VB.NET but not in C#. The alias is all you got. – Hans Passant Nov 11 '10 at 18:58
  • I second @Hans - ran into this issue the other day, myself. Incidentally, why have a class that's locked to a highly-specific business rule (less than 10 days, etc.) instead of just implementing a filter that can operate on an arbitrary date range? – Dan J Nov 11 '10 at 18:59
  • How about a nested inner class instead of a namespace? Or Something like FilterRandom.FilterLessThan10DaysGraterThan50A() instead. – mellamokb Nov 11 '10 at 19:01
  • Thanks guys. Probably a nested inner class would work. I'm probably going to use the full name in a using statement and then use the class name. djacobson - you are right; I am actually passing an array of parameters to these guys - the name is what the analyst gives it and it's easier for him to work with if I use his naming convention. We're commenting and everyone that comes along later gets it, so we're good - good idea though. I am not going to use an alias, I think that would get to be too confusing for maintenance down the road. – Mike Malter Nov 11 '10 at 20:52

1 Answers1

1

Nope, it doesn't. I know it's very irritating.

My first try at solving this issue (I had the same issue) was adding these usings:

using FilterRandom = BusinessLayer.FilterElements.FilterRandom;

The problem then becomes that you have to add one for every sub namespace you want to include, and that becomes a mess.

How I permanently solved this is by changing the namespaces in the project so that, in your example, FilterRandom would e.g. be in BusinessLayer.

The problem you are actually seeing is that you have too many namespaces. It isn't strange it happens. They are a great way of organizing your code and classes and it's not that hard to have it go out of hand. What I mean by changing the namespaces is that I merged many small namespaces into larger ones. This sometimes means renaming classes, but my opinion is that the class name on itself should be meaningful, without the namespace prefix.

This way, I permanently solved these issues in my project (60kloc) and it worked out great.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
  • Pieter, thanks but I'm going to stay away from using an alias. Might be too confusing for maintenance guys down the road. The RandomFilter is actually in the BusinessLayer project. Did you mean use a nested class? – Mike Malter Nov 11 '10 at 20:56
  • @MikeMalter - Expanded the answer. – Pieter van Ginkel Nov 11 '10 at 21:55