No Constructors Defined

I have yet another simple question. I know that in C# if you write a class and do not define a constructor the default base class constructor is defined for your class. I also know that if you write a class and then make its constructor private it will not be accessible to calling classes, therefore making it impossible to instantiate the class. The problem I am having is that certain classes in the C# language give an error of "no constructors defined" when you try to instantiate them using new.

Example if you write the line of code:

System.Windows.Forms.TreeNodeCollection tnc = new System.Windows.Forms.TreeNodeCollection();

You will get the compiler error:

The type 'System.Windows.Forms.TreeNodeCollection' has no constructors defined.

How does one write an object that is like this I realize that if I really want to instantiate a TreeNodeCollection I can get it from a TreeNode's Nodes property eg tnc = (new TreeNode()).Nodes; But what I want to do is know how to create a class with no constructor defined. If you make a constructor private the error is an access level error, not "no constructors defined." Am I missing something simple Thanks for the help.

Isaac




Answer this question

No Constructors Defined

  • Shyju

    Thank you for the reply, but I do understand private constructors. If the example I had used had a private constructor the error would have been that of an access level violation and not that there was no constructors defined. Every class that is written in C# has a constructor associated with it as far as I know, even if it is a static class. The problem is, it appears as though the class in my example breaks that paradigm because it is not a static class or abstract class and its constructor is not protected in some way. It simply does not have one. How does one compile an object of their own that would give this compiler error That is what I really would like to know.

  • knreddy

    You need to provide an internal constructor and not a private constructor for your class to get this error.

    Cheers!


  • Eureuka

    Thanks for that answer, had I not done the research to understand what you mean by that prior to getting the answer I would still be less that sure about what you mean, but I ended up trying to write my own class with a internal constructor and then compiled it into its own dll and tried to instantiate it using its constuctor and got the error I was seeing here. I did not realize that all methods that are declared as internal to an assembly are not visable to anythign outside that assembly. I thought that it would still give an access level violation and not a method not found error. Now I know. I guess this just shows my ignorance of the .Net framework and C#.

    Isaac



  • common

    Private constructors are added by design, for classes that you should not be able to construct, such as helper classes. Some classes also use a factory pattern, where static methods on the class can constructor the object, but no constructor exists. This allows the static method to return null, where a constructor would not be able to report failure without throwing an exception. For example, Bitmap has FromFile, Bitmap.FromFile("C:\b.bmp") tries to read that file and return a bitmap.



  • Carsten Thielepape

    To anwser your question, how would you go about creating an object like this...

    Look at the msdn documentation for that class here.


  • No Constructors Defined