user11910061
user11910061

Reputation:

How can I Import A Namespace from A File in A Seperate Folder in C#

How do I bring into the current namespace an extension method (see below) into scope, when the file this class is defined in is in another folder?

When StringHandler.cs is in the Product folder, I can include using Utilities; in Product.cs, but when it is in a separate folder, alongside the Project folder (in the Utilities folder), I can't figure out how to include it. Can someone please explain how I can use the using keyword in this case? For instance, where is using really pointing to on my file system (do I need to specify the using directory relative to my csproj file or to my CallCenter.sln file?)?

│   CallCenter.sln
│
├───src
│   ├───Project
│   │   │   Product.cs
|   |   |   Project.csproj
│   │   ├───bin
│   │   │   └───...
│   │   │
│   │   └───obj
│   │       └───...         
│   │
│   └───Utilities
│           StringHandler.cs

StringHandler.cs

namespace Utilities
{
    public static class StringHandler
    {
        public static string InsertSpaces(this string source)
        {
            string result = string.Empty;

            if (!string.IsNullOrWhiteSpace(source))
            {
                foreach (char letter in source)
                {
                    if (char.IsUpper(letter))
                    {
                        result = result.Trim();
                        result += " ";
                    }
                    result += letter;
                }
            }
            return result.Trim();
        }
    }
}

Upvotes: 1

Views: 5382

Answers (2)

Kit
Kit

Reputation: 21769

How do I bring into the current namespace an extension method (see below) into scope, when the file this class is defined in is in another folder?

Where the file is defined, folder-wise, is irrelevant. By convention, the folder structure on disk mimics the namespace, but it does not have to. A using "brings" a type into the namespace. (Not really "brings" -- more like "makes available to").

As far as the source type being available to the namespace in the target project, it has to be available physically to your project via a

  • direct reference to the source project from the target project
  • direct reference to a source NuGet package from the target project
  • indirect reference to the source project via a transitive dependency of another project from the target project
  • indirect reference to the source NuGet package via a transitive dependency of another project or NuGet package from the target project

and logically by

  • a using statement to the source type

When StringHandler.cs is in the Product folder, I can include using Utilities; in Product.cs, but when it is in a separate folder, alongside the Project folder (in the Utilities folder), I can't figure out how to include it. Can someone please explain how I can use the using keyword in this case?

You can't use a using to get to StringHandler as you've currently defined your structure. The reason is that StringHandler is not a part of any project. Because it's not a part of any project, it violates the rules I listed above.

To use the using as you expect, you have two simple choices in your case:

  • add StringHandler directly to the project that needs it, or if you want StringHandler to be separate, add it to another project and reference that project from the target project.
  • with a little more Kung Fu, leave StringHandler where it is (project-less) and include a link to that file in the target project. This is cheating a little bit: it makes it look like StringHandler was in Project all along. This case is rare; I personally only do this when I want to include, for example, common assembly attributes, not common code.

For instance, where is using really pointing to on my file system (do I need to specify the using directory relative to my csproj file or to my CallCenter.sln file?)?

Again, forget about the directory/folder/disk structure. Only think about the in-solution physical references (project/NuGet based) and logical references (namespace-based). They usually match the directory/folder/disk, but they don't have to.

Upvotes: 0

C.Evenhuis
C.Evenhuis

Reputation: 26456

The folder in which a .cs file is located has no meaning during compilation. The using statement just allows types from a certain namespace to be found. To be able to use the extension method, make sure that:

  • The namespace is included (using Utilities;)
  • The file (StringHandler.cs) is included in your project or in another project that your current project has a reference to

Upvotes: 1

Related Questions