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
This question doesn’t make sense.
A code file references another one through a combination of
namespace‘s and theusingdirective.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
CheckCartmethod 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.