none
Thought on constructor mismatch compiler error RRS feed

  • General discussion

  • public class Foo
    {
     // Note: no accessibility.
     Foo(int i) { /* do stuff */ }
    }
    
    public static void Main()
    {
     Foo foo = Foo(1);
    }

    In both 2008 and 2010 the resulting compiler error tells you, specifically, that Foo has no constructor that takes one argument, rather than telling you that you don't have access to this particular constructor.

    Newbie mistake, I know, but the misleading feedback doesn't help to hasten the facepalm :)

     

    Tuesday, March 1, 2011 5:54 PM

All replies

  • Hi,

    You may forgot 'new' keyword i.e. Foo foo = new Foo(1);

    Regards,

    Yasser.


    LEARN HOW TO USE WINDOWS API DURING A QUICK, SIMPLE AND PRACTICAL HOW TO:
    How To: Changing TextBox blinking caret using Windows API
    Tuesday, March 1, 2011 6:05 PM
  • Also you should make the constructor public i.e.

    public class Foo
    {
    // Note: accessibility.
    public Foo(int i) { /* do stuff */ }
    }


    LEARN HOW TO USE WINDOWS API DURING A QUICK, SIMPLE AND PRACTICAL HOW TO:
    How To: Changing TextBox blinking caret using Windows API
    Tuesday, March 1, 2011 6:06 PM
  • Hi, Yasser;

    Thanks for trying to help me fix the code, but the code is mean't to not-work. If you read the post rather than the code, you'll see that I'm talking about the wording of the resulting compiler error.

     

    Tuesday, March 1, 2011 11:03 PM
  • If you think that's bad, look at the error codes you get with a syntax mistake (e.g. a misplaced semicolon) in C :)

    ShaneB

    Wednesday, March 2, 2011 12:39 AM
  • Hi, Yasser;

    Thanks for trying to help me fix the code, but the code is mean't to not-work. If you read the post rather than the code, you'll see that I'm talking about the wording of the resulting compiler error.

    Sorry for my misunderstand, I got your issue now!

    I tried different codes and found out that: "Protection level compile-time error will be displayed if you define at least one non-private constructor.".

    For example:

    namespace ConsoleApplication1
    {
     class Program
     {
      public static void Main()
      {
       Foo foo = new Foo(1);
      }
     }
     public class Foo
     {
      Foo(int i) { }
      public Foo(string s) { }
     }
    }
    
    Error	1	'ConsoleApplication1.Foo.Foo(int)' is inaccessible due to its protection level
    
    By this time, I have no clue about this behaviour!
    LEARN HOW TO USE WINDOWS API DURING A QUICK, SIMPLE AND PRACTICAL HOW TO:
    How To: Changing TextBox blinking caret using Windows API
    Wednesday, March 2, 2011 4:30 AM