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

No Constructors Defined
Shyju
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
Look at the msdn documentation for that class here.