Reputation: 5896
I have a file, Tools.cs, with a class in it, Tools. I use another file, Default.cs, as the codebehind to my Default.aspx. I've tried all of the below to use the tools file in the default file, but nothing works. How can I do this?
using Tools;
using Global.Tools; //Added namespace Global to my tools class
using Global;
EDIT: To be more throrough, here's some code.
//Tools.cs
namespace Global{
public sealed class Tools{
//Tools stuff
}
}
//Default.cs
using System;
using System.Web.UI;
using Global;
namespace Home{
public class Default :Page{
//Page stuff
}
}
and I get the error:
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0246: The type or namespace name 'Global' could not be found (are you missing a using directive or an assembly reference?)
Upvotes: 0
Views: 1314
Reputation: 5150
Make sure your Tools class is set to compile in the Properties --> Build Action.
Also, to use it you may have to do something like this:
_Default.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThisWorks.Fine;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Tools tools = new Tools();
}
}
And
Tools.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ThisWorks.Fine
{
/// <summary>
/// Summary description for Tools
/// </summary>
public class Tools
{
public Tools() { }
//Tools stuff
}
}
Upvotes: 1
Reputation: 3528
If you are trying to reference it within the Default.aspx, edit your web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<pages>
<compilation debug="true" targetFramework="4.0" />
<namespaces>
<add namespace="WebApplication1.SubNamespace"/>
</namespaces>
</pages>
</system.web>
</configuration>
and your class may look like this:
namespace WebApplication1.SubNamespace
{
public static class Class1
{
public static string value { get { return "hello world!!"; } }
}
}
this will make it referable within aspx markup like this:
<form id="form1" runat="server">
<div>
<%= Class1.value %>
</div>
</form>
Upvotes: 1
Reputation: 5251
OK, since you're developing in Notepad++ are you compiling using csc?
The syntax for the compilation should be something like:
csc /t:library /out:MyCodeLibrary.dll Tools.cs Default.cs
To see the options for csc look at: http://msdn.microsoft.com/en-us/library/ms379563%28v=VS.80%29.aspx
Note that you can download Visual Studio Express for free and it'll make you much more productive and reduce the confusion in cases such as this.
Upvotes: 0
Reputation:
The using
directive isn't for files, it's for namespaces.
using YourToolsNamespace;
Where YourToolsNamespace
is what contains your Tools
class.
Edit: you will also need to have this in your project references. For instance, you can reference your code library from the GAC, another project, or a specific location. Right-click on references and find your assembly there.
Upvotes: 3
Reputation: 4255
C# uses namespaces to separate classes. If you put both classes inside the same namespace, they'll be accessable to each other. Otherwise, use a using
declaration as in your answer, but specify the namespace, not the filename.
//Tools.cs
namespace MyApp.CoreLogic
{
class Tools
{
...
}
}
Then...
//Default.cs
namespace MyApp.FrontEnd
{
using MyApp.CoreLogic
class Default : Page
{
...
}
}
Upvotes: 0