Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't see a referenced class of another namespace in C#

I have 2 projects:

  • ConstrainedScheduleInterfaces
  • ConstrainedSchedule that has a folder (Testing) with my class that need the reference, here's the code:

Tests.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 
using NUnit.Framework;
using Ninject;
using ConstrainedScheduleInterfaces;

namespace ConstrainedSchedule.Testing
{
    internal static class Configuration
    {
      ...........
    }
}

I added the reference to the ConstrainedSchedule project, but the using ConstrainedScheduleInterfaces; is marked red as not found. Both the project has destination framework set .NET Framework 4.5 Any help? Thanks

like image 533
Kay90 Avatar asked Jan 03 '14 16:01

Kay90


People also ask

Can a namespace contain another namespace?

A namespace can contain another namespace. It is called nested namespace. The nested namespace and its members can also be accessed using the dot (.) operator.

Can partial classes be in different namespaces?

All parts of a partial class should be in the same namespace. Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files of a different class library project.

How do I use a namespace in another project?

First, you need to add the project which contains the utility operations as a reference to the project which will be using them. If you're using Visual Studio Code or command line, you can use dotnet add reference command to do that, and in Visual Studio you can use Reference Manager.

Can namespace and class have same name?

Inside a namespace, no two classes can have the same name.


2 Answers

Does the project contain a reference to the other project? You can't just add the namespace, the project itself needs an assembly reference to the other project which has that namespace.

In Visual Studio, open the Solution Explorer. Right-click on the ConstrainedSchedule project and select something along the lines of "Add Reference." In the dialog, select project references (depending on the version of Visual Studio it may be a tab or some other interface indicating projects as part of the solution). Select the ConstrainedScheduleInterfaces project and add the reference.

More information here.

like image 113
David Avatar answered Oct 07 '22 21:10

David


For other people who have this problem, who have already added the reference to the dll and have made sure you have the right namespace at the top, I've found another thing that can cause this.

Select-click the file in visual studio and choose properties. In the Advanced part of properties, look for Build Action. Change it to compile if it's not already on that, and try to build again.

like image 27
jimboweb Avatar answered Oct 07 '22 21:10

jimboweb