Search code examples
c#classtypesnamespaces

'namespace' but is used like a 'type'


My program uses a class called Time2. I have the reference added to TimeTest but I keep getting the error, 'Time2' is a 'namespace' but is used like a 'type'.

Could someone please tell me what this error is and how to fix it?

    namespace TimeTest
    {
      class TimeTest
      {
        static void Main(string[] args)
        {
            Time2 t1 = new Time2();
        }
      }
    }

Solution

  • I suspect you've got the same problem at least twice.

    Here:

        namespace TimeTest
        {
            class TimeTest
            {
        }
    

    ... you're declaring a type with the same name as the namespace it's in. Don't do that.

    Now you apparently have the same problem with Time2. I suspect if you add:

        using Time2;
    

    to your list of using directives, your code will compile. But please, please, please fix the bigger problem: the problematic choice of names. (Follow the link above to find out more details of why it's a bad idea.)

    (Additionally, unless you're really interested in writing time-based types, I'd advise you not to do so... and I say that as someone who does do exactly that. Use the built-in capabilities, or a third party library such as, um, mine. Working with dates and times correctly is surprisingly hairy. :)