-1

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.

Graf123456
  • 91
  • 1
  • 1
  • 8
  • 1
    if it's in the same namespace you should be able to use it without a `using` – Patrick Beynio Apr 26 '22 at 13:06
  • Probably worth right-clicking the project, Properties, check which version of C# it's targeting and make sure it's the correct one. As @PatrickBeynio says, you shouldn't need a `using` for the same namespace. `using static` is a C# 6 feature. – Basic Apr 26 '22 at 13:07
  • It's using Web Forms and it was probably created as a web site, rather than a web application. I don't think those have project files to begin with. – user18387401 Apr 26 '22 at 13:08
  • 1
    do not use `using static Bum.Iglavci.Smreka.Logger`. Use static should be only used if classes are very closely related, and its a good practise to follow. You should be able to use Logger without using, but in your case normal `using Bum.Iglavci.Smreka` is more than enough, and probably Visual Studio is gonna point out that mistake, since they are in same namespace and you dont need to import it. Also if you still want to use `using static` functionality you can change c# language version in project properties. – Bartosz Olchowik Apr 26 '22 at 13:08
  • also, do not name folders same as class files. It may lead to naming issues- you cant define a namespace called Smerka and define 'smerka' class name inside Smerka namespace. Its not gonna work when you will want to build it or import it anywhere else, due to naming issues. – Bartosz Olchowik Apr 26 '22 at 13:11
  • Yes, i know that it should work without using "using" at all, however when i write Logger in to smreka.cs it just gets a red underline that it has no idea what it is. Compiler really doesn't seem to see it. There are a lot of errors in the solution due to some libraries being out of date and my job is to update those libraries. Is it possible that the import issue will be resolved once i fix all the other errors? However none of the errors are in log.cs or in smreka.cs (aside from Logger not being recognized). – Graf123456 Apr 26 '22 at 13:14
  • @Graf123456 change class name from smerka to different name and it is gonna work. How C# should know if u want a namespace or a class, if they are named equal ? – Bartosz Olchowik Apr 26 '22 at 13:15
  • @BartoszOlchowik but smreka.cs doesn't have a class called smreka. I it's a large file with some other classes in it. Also log.cs has only a class called Logger so the names are different. – Graf123456 Apr 26 '22 at 13:19
  • @Graf123456 put entire code here then. without that i cant tell u exactly what you need to change, and im almost sure its due to naming problems. – Bartosz Olchowik Apr 26 '22 at 13:21
  • @BartoszOlchowik it's really long to dump everything but i've tried to mimic with what the issues is in the Edit. – Graf123456 Apr 26 '22 at 13:38
  • Could you please also provide the exact compiler error message? – Zdeněk Jelínek Apr 26 '22 at 13:45
  • You might also increase build verbosity and check the logs of what happens. But without full repro, we can hardly help you, as there does not seem to be a superficial problem. If the C# wasn't compiling the project at all, you would not get the C# version error for static imports. Maybe you could check if the logger file is not excluded from build but these really are just guesses. – Zdeněk Jelínek Apr 26 '22 at 13:47
  • @ZdeněkJelínek it simply says `The type or namespace 'Logger' could not be found (are you missing a using directive or an assembly reference?)` – Graf123456 Apr 26 '22 at 13:50
  • Please also edit it into the question so that future readers get all info from the posting directly. – Zdeněk Jelínek Apr 26 '22 at 13:51
  • @ZdeněkJelínek i've added the error to the edit. It's actually a possibility that it's not added to the build since i've added the files manually to the project. Can you point me in to the direction where i could read up on how to check this? – Graf123456 Apr 26 '22 at 13:58
  • I'd say https://stackoverflow.com/q/219417/3593970 and https://stackoverflow.com/q/1211841/3593970 might be relevant pieces for what I mentioned. – Zdeněk Jelínek Apr 26 '22 at 14:58
  • 1
    You need to provide [a minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) here. Have you solved the problem so far? – Jiale Xue - MSFT Apr 27 '22 at 02:02
  • @Graf123456 You can check warnings in Visual Studio by clicking on the `View` menu, and then `Error List`. I would advice you to look for naming issues, or warnings connected with dll references. Post a warning here if it will be suspicious, or when u wont understand it. – Bartosz Olchowik Apr 27 '22 at 06:36
  • @JialeXue-MSFT no i have not solved the problem. I will still be doing some more testing tomorrow. – Graf123456 Apr 27 '22 at 17:05
  • @BartoszOlchowik i'll check up on some errors as well. I did notice one that is very weird and will post it tomorrow. – Graf123456 Apr 27 '22 at 17:06

4 Answers4

0

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
        }
    }
}
Roe
  • 633
  • 2
  • 14
  • I've tried to update my post to mimic how it looks like in the smreka.cs and log.cs files. From what i can see i think it's implemented correctly, that's why i'm more asking if it's related to something being wrong with the project itself. – Graf123456 Apr 26 '22 at 13:44
0

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

Roe
  • 633
  • 2
  • 14
0

I found no simple solution. I now created a new .net framework application project and added the files in to the new project. For some reason the namespace works correctly now and the files can see each other in the same namespace.

Graf123456
  • 91
  • 1
  • 1
  • 8
  • Hi Graf123456, glad to know you've found the solution to resolve this issue! Please consider accepting it as an answer to change its status to Answered. It will also help others to solve a similar issue. See [can I answer my own question..](https://stackoverflow.com/help/self-answer), Just a reminder :) – Jiale Xue - MSFT May 02 '22 at 09:00
0

Yes the error comes from the fact that you don't have a .csproj file.

Such files contain the list of files to compile when building a project. Just having the solution file is not enough.

I suggest some readings on project and solution using Visual Studio :

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48