Home > Software design >  Can't import classes from same namespace
Can't import classes from same namespace

Time:04-28

I'm fairly new to .NET and I'm trying to get an old program to work that no longer has it's .csproj file. I've managed to receive an old .sln file from the creator and opened the solution in VS. From what I can see this is a Developer Web Server project?

enter image description here

Here is the issue.

In the folder Smreka there are 2 files, log.cs and smreka.cs. The log.cs contains the implementation of a class Logger, which I am trying to import in to smreka.cs. They are both using the same namespace Bum.Iglavci.Smreka so as far as I know, I should be able to import the Logger class without any issues. The problem is that the compiler just can't see it. If I try to directly import it with using static Bum.Iglavci.Smreka.Logger;, I get an error Feature 'using static' is not available in C# 5. Please use language version 6 or greater.

I would like to know why the namespace can't see each other. Is it because I'm missing the .csproj file? Does Developer Web Server even need a .csproj file? If so what's the best way to generate one?

EDIT: Due to some confusion I'll try to add more details regarding how log.cs and smreka.cs look like. The files are actually a lot longer but I think this should give an idea.

log.cs:

namespace Bum.Iglavci.Smreka{

public class Logger{
    public Logger(){
        
    }
    public void DoSomething(){}
}
}

smreka.cs:

namespace Bum.Iglavci.Smreka{

public class Baza{
    private Logger log;
    public Baza(){
        log = new Logger();
    }
}
}

The compiler has no idea what Logger is under property private Logger log; It states the error The type or namespace name 'Logger' could not be found (are you missing a using directive or an assembly reference?)

I think the namespace is correctly placed, that's why i have a feeling there's something wrong with the project or the solution itself that i need to fix.

CodePudding user response:

Since both classes are in the same namespace they are already able to use each other. You can acces the class by simply doing the following. Let's take Log as the class to call the other class.

Log class:

namespace Bum.Iglavci
{
    public class Log
    {
        public static void ExecuteDoSomething()
        {
            Smreka.DoSomething();
        }
    }
}

Smerka class:

namespace Bum.Iglavci
{
    public class Smerka
    {
        public static void DoSomething()
        {
           //execute code here
        }
    }
}

CodePudding user response:

It could be possible that the files have the Buil Action property set to None this will not compile the files. Set it to C# Compiler, this should solve it.

If you don't know how to acces the properties of a file.

  1. Right click the file
  2. Navigate to Properties in the bottom of the list
  3. Set the Build Action to C# compiler (see image)

enter image description here

  • Related