C# WinForms, what is this strange error?
I am new to C# WinForms. I have made a class (EnhancedForm) which inherits Form class. In EnhanceForm I have put some common functions (开发者_如何学Pythonlike validations, common used objects like DataTable and string variables etc, so that I may not have to code same again and again in forms) that I want to use in all forms and next all of my forms inherits this (EnhancedForm).
In DAL (a separate class library) I have a class CommonDB class which have DB related common functions. For database communications I was making an object of CommonDB in all forms. I thought to put CommonDB in EnhancedForm so that I may not have to make its objects in all classes.
Here is something happening that I couldn't understand. Application runs fine. But if I try to open a form in design view it gives an error. "Object reference not set to an instance of an object. " I couldn't view a form in designed mode. If I remove CommonDb object from EnhancedForm and put it in individual forms it works fine and allow to view forms.
If I compile and run this error do not appears. Please guide and help me in this regard.
Thanks
You have code in EnhancedForm that assumes a CommonDb object. At runtime you create that (probably in Form_Loaded) but Design-time it is absent.
Preferrably, all your code accessing CommonDb should check for null, and/or this.DesignMode
It seems as if you are initiating your CommonDB class inside your base form's constructor. Visual Studio uses Reflection to show forms in design view, that causes the form's constructor to be called. You could try just rebuilding the project to see if you can see the forms in design view but I do not think it will work.
精彩评论