-1

In C#, how can I use classes from local C# files?


In my chatterbot.cs file I tried using ChatterBotAPI.ChatterBot but I got the error

System.InvalidOperationException: The type or namespace name `ChatterBotAPI' could not be found. Are you missing an assembly reference?

I have a directory structure like this:

.
├── RunProject.exe
├── bot-api
│   ├── README.md
│   ├── dotnet
│   │   ├── ChatterBotAPI
│   │   │   ├── AssemblyInfo.cs
│   │   │   ├── ChatterBot.cs
│   │   │   ├── ChatterBotAPI.csproj
│   │   │   ├── ChatterBotFactory.cs
│   │   │   ├── ChatterBotSession.cs
│   │   │   ├── ChatterBotThought.cs
│   │   │   ├── ChatterBotType.cs
│   │   │   ├── Cleverbot.cs
│   │   │   ├── Pandorabots.cs
│   │   │   └── Utils.cs
│   │   ├── ChatterBotAPI.sln
│   │   ├── ChatterBotAPITest
│   │   │   ├── AssemblyInfo.cs
│   │   │   ├── ChatterBotAPITest.csproj
│   │   │   └── Main.cs
│   │   └── ChatterBotAPITestVB
│   │       ├── Application.vb
│   │       ├── AssemblyInfo.vb
│   │       └── ChatterBotAPITestVB.vbproj
└── chatterbot.cs

How do I use classes from the ChatterBotAPI in my chatterbot.cs file?


Note: I am not compiling with the VS IDE. I am using RunProject.exe, which is a C# .NET binary which runs C# scripts on the fly. It loads and runs chatterbot.cs

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • 1
    have you added a correct namespace with the `using` keyword? – Paweł Łukasik Dec 20 '16 at 07:13
  • 2
    *When* do you get this error? The exception means it's runtime, but the error should have been caught at compile time. That's strange. – nvoigt Dec 20 '16 at 07:13
  • @nvoigt I am not compiling, `RunProject.exe` is a C# .NET binary which runs C# scripts on the fly. It loads and runs `chatterbot.cs`. – theonlygusti Dec 20 '16 at 07:24
  • 1
    Then post it's code, we cannot guess what an unknown executable may or may not be doing. – nvoigt Dec 20 '16 at 07:36
  • Changing "Important note" to "note" really doesn't tell us anything about what RunProject.exe does, and that's the *important* point. A question which is "I'm running an executable that I'm not going to give you details of, and it's not doing what I want it to" is pretty hard for anyone to answer. – Jon Skeet Feb 04 '22 at 16:06

2 Answers2

0

You have to open the project that is going to use the classes and add a reference to the file containing the classes.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • No thank you, I am not in VS. Please, why do people fail so hard at giving non-IDE-specific answers? – theonlygusti Dec 20 '16 at 07:24
  • @theonlygusti: Maybe because you didn't specify it ;) Seems like an important detail. – Stormenet Dec 20 '16 at 07:29
  • How do I add a reference without Visual Studio? – theonlygusti Dec 20 '16 at 07:31
  • You will need to consult the documentation for RunProject.exe. – John Wu Dec 20 '16 at 07:34
  • @JohnWu there must be another way. Why can't I just `#include` a file like in almost every other language? – theonlygusti Dec 20 '16 at 07:37
  • 1
    @theonlygusti: why do people fail so hard to comprehend the features and limitations of the language they have chosen... and then argue when someone points them out. – Sam Axe Dec 20 '16 at 07:40
  • @SamAxe maybe some people have only been using the language for a couple weeks and are used to more lenient interpreted languages and compiled languages with pre-processors. – theonlygusti Dec 20 '16 at 07:42
  • @SamAxe and it isn't a limitation, there _must_ be another way. E.g. copy + pasting all code from `ChatterBotAPI` into `chatterbot.cs` might be a possibility, but idk how to actually do that. – theonlygusti Dec 20 '16 at 07:44
  • 2
    You are right, there is another way. Open the csproj files in Visual Studio, set a reference, and compile them using the IDE. If you are asking how to do it with RunProject.exe, you will need to ask someone with knowledge of RunProject.exe. This is not a general c# question. – John Wu Dec 20 '16 at 07:55
0

Unlike in Java, directory structure isn't very important and isn't coupled to "package" (namespace etc.) structure.

Most of your source files will look like this, with their contents wrapped in a namespace block:

ChatterBot.cs

namespace ChatterBotAPI
{
    class ChatterBotFactory()
    {
        ....
    }
}

As long as your code adds that namespace with using, then it can access members of that namespace without even having to fully-qualify them:

chatterbot.cs

using ChatterBotAPI;

...

var botFactory = new ChatterBotFactory();
var bot = botFactory.Create(ChatterBotType.CLEVERBOT);

If the code you are trying to use is in another project, you will have to reference it.

The best way to develop C# and what I'd recommend you do from now on is inside of projects using the dotnet-cli instead of standalone files using csc, then you will be able to reference resources and build with options more easily.

Do

$ mkdir MyProject
$ cd MyProject
$ dotnet new console

(there are other types of project templates available, console is for CLIs/Console Applications)

Then in MyProject.csproj, underneath the entire <PropertyGroup>...</PropertyGroup> section, add a relative reference to the other project's csproj:

<ItemGroup>
  <ProjectReference Include="..\dotnet\ChatterBotAPI\ChatterBotAPI.csproj" />
</ItemGroup>

now edit Program.cs to your heart's content (probably fill it with your current chatterbox.cs's contents) and run dotnet build and it should all work.

theonlygusti
  • 11,032
  • 11
  • 64
  • 119