tags:

views:

1045

answers:

3

I sometimes define Business Logic classes to "help" my ASPX code-behind classes. It makes the most sense to me to include them both in the code-behind file since they work together. However, I'd occasionally like to access the Business Logic classes from higher level classes defined in App_Code but they aren't automatically accessible outside of the file.

Thus, the question: it is easy to access classes defined in App_Code but how do I access classes defined elsewhere?

UPDATE: One other thing, the ASPX page class and the App_Code class are in the same namespace - that isn't the issue.

NOTE: I have taken the advice of those who have responded (thanks guys) and am refactoring to make class access easier. However, I don't think the question is actually answered yet (in the case of an ASP.NET Website project). I don't need the answer any more but, if someone could clarify what makes classes visible when they are outside of App_Code, it may well help someone else (or even me, down the road).

+5  A: 

Make sure you place your classes in a sensible namespace.

Place 'using' keyword in code behind files you would like to access them.

Or <%@ import if you are using them in inline code.

Put the dll that contains your classes in the /bin folder.

TBH I prefer to keep the separate library project in the same solution and have project reference in the Web probject. VS handles building and placing the dll for you.

AnthonyWJones
Thanks Anthony. I don't need the using keyword (or import) because these classes are in the same namespace and project). I think you may be right about a separate library project - especially for my DAL. If my business logic was in a separate project it would make nUnit testing easier too...
Mark Brittingham
+1  A: 

I assume you mean you are defining a separate class inside the codebehind .cs file? What access modifiers are you giving them?

As above though, I'd generally have a separate project in the solution for this kind of thing (usually in a different namespace like MyApp.Web and MyApp.), and just reference it from there.

Steven Robbins
Yes - it is a separate class inside the codebehind .cs file. They are all public. These "helper" classes provide page-specific business logic but I'm a bit loath to place them all in a separate project because they deal so intimately with the needs of the corresponding page.
Mark Brittingham
A: 

You can not access a class from another class in same code behind file as .net dsnt support multiple inheritance. but you can create you business logic class as inner class in main class and make its all functions public so that they can be call in main class.

Nitin Hooda
I don't think you quite got the gist of the question. I wasn't trying to do multiple inheritance. First, you can have more than one class in a code behind and one can call into the other. It appears that you just can't access a class in one codebehind from another codebehind. Honestly, I don't even do this...I was just curious as to whether this is a hard-coded constraint of the language. At this point, I have most classes defined in a Business Logic layer anyway and no such constraints exist there. But thanks for trying!
Mark Brittingham