Reputation: 153
I've got an uncompiled site using C#. I've got 6 files sharing a bunch of functions in random classes. I'd like to move these into a more logical separation, but I don't see how to refer to classes in another cs file.
Ideally, I'd separate these into cs files into logical pieces like shoppingcart class in one file, routine functions in another, etc. And all 6 files can refer to any of the separated cs files.
Is this even possible if we don't compile them?
update:
Ok, so my code might look like this and the syntax for the ShoppingCart.cart is not correct but it's an example of how I'd like this to work:
default.aspx.cs:
using ShoppingCart;
public partial class DefaultPage : System.Web.UI.Page
{
protected void page_load()
{
ShoppingCart.cart.CheckCart("newguy");
}
shoppingcart.cs:
namespace ShoppingCart
{
public partial class cart : System.Web.UI.Page
{
public string CheckCart(string cartname)
{
return "test";
}
etc
Upvotes: 0
Views: 756
Reputation: 88072
Is this even possible if we don't compile them?
This question doesn't make sense.
A code file references another one through a combination of namespace
's and the using
directive.
Your example kind of shows this. However, it's completely invalid code (won't compile) because you don't have an instance of the cart class to reference. It was never instantiated.
Which means you need to mark the CheckCart
method as static, turn the cart class into a singleton, or make DefaultPage descend from cart.
Regardless, the ShoppingCart namespace needs to be compiled, which means those 6 files you are talking about need to be grouped into an assembly OR be made part of your main project in order to get the code to run.
Web Site Projects do some of this completely behind the scenes if you just drop the code into the app_code folder. However, WSP's do a number of other unexpected things which if you've worked with them for any length of time you'll come to find out are utter garbage.
So, building on John's comment, convert it to a Web Application Project. For the additional code you have the option of creating a separate assembly project and throwing it in there or simply leaving it in sub folders of the main project.
Upvotes: 2
Reputation: 63095
if you need to separate the business logic from UI, one option is creating separate class library for business logic and call those methods from UI layer. following link will help you
Creating a Web Application Project with a Class Library
Or you can keep them in same project but make sure you are using same namespace for all the classes.
Upvotes: 1
Reputation: 5681
Keep them in the same namespace. This will let you call other classes from other classes......
And your cs-files do get compiled. It just happens under the hood ;)
Upvotes: 1