0

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?)

3
  • 2
    I would strongly recommend using a namespace called something other than "Global". Commented Feb 23, 2012 at 19:17
  • +1 Normally you will want to name namespace, parameter, property to something not a keyword or commonly use in programming language. Commented Feb 23, 2012 at 19:21
  • Yeah, in the final code I plan to. This doesn't fix anything though. The issue is, I believe, the Tools.cs isn't building. I'm doing all of this in notepad++ on a shared folder that reconciles to our webserver. Commented Feb 23, 2012 at 19:24

6 Answers 6

3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Yes, I know this. I've added the namespace Global around my class. But there has to be a reference to the file somehow, or the namespace isn't recognized. I've edited the above post to include a more thorough example and error code.
If you type "Global." (without quotes) does the intellisense recognise Tools as within that namespace? Are both classes in the same project? Is there any scope definition on Tools e.g. private/public?
There is no intellisense, no right click on references, or anything like that. I'm building this as raw files in notepad++ without any bells or whistles due to a lack of software. How do I add the reference so it knows to compile the Tools.cs so that the namespace is even visible?
@steventnorris Do you not have the ability to download the free version of Visual C#? It'll save you a lot of heartache.
@steventnorris I won't keep beating a dead horse, but Visual C# is free and you can easily install Visual Studio on top of it no problem.
|
1

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>

5 Comments

Where can I find the web.config?
Its in the root of your web project. Is this your first .NET web app? :-)
No, it's the third or so, but I'm creating everything by hand so I have no web.config unless I make it (no Visual Studio yet. Waiting on an IS install so my hands are tied.)
oh ok :-) you are notepadding it I guess. I added the basic file to the answer. hope it helps!
Thanks. I think it's a build problem now though, based on some research. I see namespace references, but my problem is the Tools.cs isn't even compiling into a workable dll i believe.
1

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
    }
}

7 Comments

How do I set it to build? I'm using notepad++ to create this files raw. See above code edit for reference.
@steventnorris - If you are using notepad++ to create the files, how are you compiling? Can you post the command that you are using to compile the code?
The server compiles on the fly thus far using the codebehind and src tags. I could create a config file to compile maybe so i don't have to command line compile everthing every time???
@steventnorris - Please look at my changed code. It seems to work for me when i changed the namespace from Global to ThisWorks.Fine.
I'd imagine you're using an IDE like vis studio or something that compiles the .cs into a .dll and places it in the correct folder for you. I do not have this luxury right now, unfortunately. Is there a config file I can create to force the .cs to build to a .dll at the server level, just like the codebehind and src directives do?
|
0

Make sure your Tools class is public.

2 Comments

Ah, I see - sorry, I don't have experience creating applications outside of Visual Studio in this manner. By the way, there are free versions of Visual Studio: microsoft.com/visualstudio/en-us/products/2010-editions/express
Waiting on a clerance for spending. I'll have vis studio soon hopefully.
0

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
  {
    ...
  }
}

Comments

0

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.

2 Comments

Hmm, I'm using a shared folder though, so my command line run wouldn't be on the server (not a server admin at the moment). Is there a config file that can be created to tell the server to compile itself?
I'm not aware of how this can be done remotely. But the source code shouldn't usually be on the server, only the aspx files and the compiled dll - you can create your project locally and then push it to the server to run. For a web project using Visual Studio you can run a local development web server, which will make you much more productive than developing in Notepad. Look at msdn.microsoft.com/en-us/library/ms228041%28v=VS.85%29.aspx for how to develop ASP.NET; it may give you some assistance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.