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

31,394

Solution 1

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.

Solution 2

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.

Solution 3

There might be another reason: different target frameworks. My main project had ".NET Framework 4.7.2" and the referenced project (linked from another solution) had ".NET Framework 4.5.1".

Select the project properties and change the target framework.

Solution 4

I was having this issue after renaming a project and namespace due to a naming conflict.

Basically my main project had a reference to a project I made called Common, but then I needed to reference another project called Common that another dev had written. After renaming my project something like SpecificCommon, I was able to have references to both Common and SpecificCommon in my main project. However, all references to SpecificCommon namespace were now broken.

At first I thought this was some problem with the namespace refactor, but it turns out that it was because SpecificCommon and Common both still had the same assembly name (Common.dll). Changing the assembly name in SpecificCommon's properties and rebuilding solved the issue for me.

Share:
31,394
Kay90
Author by

Kay90

I'm a computer science student and pianist!

Updated on July 09, 2022

Comments

  • Kay90
    Kay90 almost 2 years

    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

  • Kay90
    Kay90 over 10 years
    Yeah, I had already added the reference on the ConstrainedSchedule project. I make sure that the other project haven't the reference to the other. Ah, I have VS2012.
  • David
    David over 10 years
    @Kay90: Additionally, is there an actual namespace called ConstrainedScheduleInterfaces? The project may be called that, but that doesn't guarantee such a namespace exists. Are you able to reference the type name by fully-qualifying it? What is its fully-qualified name?
  • Kay90
    Kay90 over 10 years
    Ah! Thanks!!! In the ConstrainedScheduleInterfaces files I found that the namespace had another name! Thank you very much!
  • Wungsow
    Wungsow almost 3 years
    This was it for me, I had two projects with different names as references but they both output the same assembly name Common.dll. Wouldn't have thought to check that.
  • Darryn Frost
    Darryn Frost almost 3 years
    This was my problem. For some reason, when I added a couple of class files, they got marked as "content" and I did not realize it. Was driving me nuts.