0

I have the following code (which compiles and works fine if I leave the warning in, I haven't tested it using the global:: hack):

namespace NotifierService.Models {}

namespace NotifierService
{
    using Models;
}

The 'using Models' statement is underlined saying (in the Error List this is only a warning level issue):

Using directives for namespace 'NotifierService.Models' should be qualified

So I then qualify it as:

using NotifierService.Models;

and get the following Error level issue:

The type name 'Models' does not exist in the type 'NotifierService'

P.S. I know I can 'hack' this to remove the warning / error by using the following but I'm trying to understand what/why it is happening as this effect is app wide affecting multiple namespaces.

using global::NotifierService.Models;

ANSWER: Fildor gave me one solution... There was an issue because in my Windows Service the base class used the same name as the namespace, so by renaming all namespaces the application compiled without issue (once I had fully qualified the using statements.

However I just realised a neater solution (Because I prefer the namespaces to have the same name as the project because it makes future referencing more logical/maintainable):

Leave namespaces unchanged, and where I'm using classes from the NotifierService.Models namespace I simply reference them as follows:

List<Models.TaxiModel>

Which allows me to remove the 'using Models'... Still seems a bit strange and 'hacky' though.

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • 1
    Do you have a class `NotifierService` ? – Fildor Dec 06 '19 at 14:07
  • Where are these two namespaces? In separate projects, or separate files or are they in the same file? – MindSwipe Dec 06 '19 at 14:12
  • @Fildor I think you maybe on to something... I use namespace NotifierService { public partial class NotifierService : ServiceBase } – Paul Zahra Dec 06 '19 at 14:18
  • the only reasons i think of that gives the error is that NotifierService.Models is in different project. – Davit Mikuchadze Dec 06 '19 at 14:21
  • @MindSwipe They are in same project, different files. It is a Windows Service - See my partial class in previous post. – Paul Zahra Dec 06 '19 at 14:21
  • you cant have instance members in a static class... make `lateTaxi` static – Trae Moore Dec 06 '19 at 14:24
  • 2
    @TraeMoore lol well spotted, I've quickly constructed this code, to illustrate my issue, my bad. – Paul Zahra Dec 06 '19 at 14:25
  • which IDE are you using? VS with Resharper? whats the high level of your env? I may be wrong, but im kinda getting a vibe that this might have something to do with your env. – Trae Moore Dec 06 '19 at 14:29
  • 1
    Just for curiosity: Would you mind renaming the `NotifierService` _class_ and try if the error persists? I've had issues with same-name namespace and class before. Can't remember what it was exactly. It was more convenient to rename the class anyway. – Fildor Dec 06 '19 at 14:29
  • 1
    @Fildor I left the class as it's more fundamental to the service architecture, however I renamed all 'namespace NotiferService' entries to 'namespace Notifier' and the issue is gone!... this is weird because I do the same in multiple other applications (namespace and class with same name for a windows service base) and there are no issues in them, just checked a few projects). – Paul Zahra Dec 06 '19 at 14:35
  • 1
    @Fildor If you want to forumulate your posts as an answer I'll give you it... Just found an even easier way for it to work... I'll edit my question now. – Paul Zahra Dec 06 '19 at 14:44
  • I would write an answer if I could explain what happens and why. I was only able to help out of experience, not knowledge. So all I could write is "Having namespace and a class in it have the same name may cause weird errors." - Not really meeting my own standards for a good answer. But actually, you could write down your solution as an answer. I'd really be interested. – Fildor Dec 06 '19 at 14:49
  • 1
    An answer, according to the guidelines, can simply be something that is helpful/points the person asking the question in the right direction. – Paul Zahra Dec 06 '19 at 14:50

1 Answers1

2

I don't exactly know why it happens but having Namespaces and a class in it have the same name can cause some issues.

Sadly, I haven't kept the project where it happened to me, so I cannot reiterate the issue I had. I just know it was an issue and it was because of the identical names.

The solution on that project was to rename one of the namespace or class. If I remember correctly we renamed the class.

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • 1
    It could be... I found several other different issues linked to this message, and several do state that the message is very inaccurate / unhelpful / misleading... Anyways thanks for your help! Have a good weekend! – Paul Zahra Dec 06 '19 at 15:00
  • Just googled it, doesn't seem so. It's about the using directive itself. Removed that part. – Fildor Dec 06 '19 at 15:04