2

I am having some namespace issue that is confusing me why it is happening.

In the below code, System.IO & System.Reflection is attempting to reference abc.System instead of using the System namespace I declared at the top. Why is that?

using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace abc.Data
{
public sealed class Access
{
    public static void Open(string dbPath)
    {
         // error here referencing abc.System in System.IO, and System.Reflection.

         string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);         }
}

I then have another namespace in a separate file as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace abc.System
{
     public static class DateTimeExtensions
     {
    // Implemented from
    // http://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week
    public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
    {
        int diff = dt.DayOfWeek - startOfWeek;
        if (diff < 0)
        {
            diff += 7;
        }

        return dt.AddDays(-1 * diff).Date;
    }

    public static DateTime EndOfWeek(this DateTime dt, DayOfWeek startOfWeek)
    {
        int diff = dt.DayOfWeek - startOfWeek;
        if (diff < 0)
        {
            diff += 7;
        }

        return dt.AddDays(diff).Date;
    }
}
}
Tim
  • 14,999
  • 1
  • 45
  • 68
YouAreSalty
  • 105
  • 10
  • What do you mean by "System.IO & System.Reflection is attempting to reference abc"? – Brian Rasmussen Oct 21 '14 at 16:08
  • After answering, I realized this is probably a duplicate of http://stackoverflow.com/questions/5681537/namespace-conflict-in-c-sharp – Tim Oct 21 '14 at 16:10

1 Answers1

7

Because there's the namespace collision, you'll need to use the global keyword to make it clear what you're trying to access.

string appPath = global::System.IO.Path.GetDirectoryName(global::System.Reflection.Assembly.GetExecutingAssembly().Location); 

Or, change your namespaces if you can, because that's going to get annoying really fast!

Tim
  • 14,999
  • 1
  • 45
  • 68
  • But why is that a namespace conflict? Why wouldn't referring to System, simply be the System namespace and abc.System, be the abc.System namespace? Why would referring to System, be abc.System? – YouAreSalty Oct 21 '14 at 16:24
  • 1
    because local namespaces always take precedence. So when you type `System` it looks inside `abc.Data` (your current namespace) and doesn't find anything, then backs up one level to look at `abc` and, because of that other class you show, it finds a namespace called `System` within the namespace `abc.` So it uses that, which wasn't your intention. – Tim Oct 21 '14 at 16:26
  • 1
    FWIW I avoid keywords like crazy for all languages (although it's more an issue in SQL) – Nyra Oct 21 '14 at 16:27
  • I see. So what do people do when they have such conflicts, because I imagine this happens often and using the alternative syntax isn't pretty? – YouAreSalty Oct 21 '14 at 16:27
  • 1
    Kind of like @alykins said - avoid using keywords (or things like keywords - such as the default set of namespaces). Having your own System namespace (especially since its not top-level) is going to be an issue. I'd just avoid that. – Tim Oct 21 '14 at 16:30
  • Thank you for everyone's help! I changed the name to abc.Calendar and will steer clear of keywords. – YouAreSalty Oct 21 '14 at 18:48