C # how to solve the problem that the object reference is not set to the object instance during Interface Design
When you are working on a. Net project, you often encounter the problem of not setting the object reference to the object instance during the design interface. The solution is as follows:
1. The Winform and Asp. Net interfaces can be designed as follows:
Winform project:
Protected override void OnLoad (EventArgs e) {base. onLoad (e); if (string. compare (Process. getCurrentProcess (). processName, devenv) = 0) return; // other related code, or // if (string. compare (Process. getCurrentProcess (). processName, devenv )! = 0) // {// other related code, //} // or // if (! This. DesignMode )//{}}
Sometimes some operations must be added to the constructor (although this is not recommended), this problem also occurs, and the write in the constructor is slightly different.
Public BuroHomeFrm () {InitializeComponent (); if (string. Compare (Process. GetCurrentProcess (). ProcessName, devenv )! = 0) {// other code ..... }
Or
// If (! This. DesignMode) // {Other code} // if it is still written in that way, an error is returned. // If (string. Compare (Process. GetCurrentProcess (). ProcessName, devenv) = 0) // return ;}
Asp. Net
2. for WPF, you can refer to the above writing method. There is also another writing method.
If (! DesignerProperties. GetIsInDesignMode (this) {// other code}