C # Design interface, object reference not set to object instance problem solution

Source: Internet
Author: User

In doing. NET project, you often encounter problems when designing the interface, where object references are not set to object instances, and the solution is given below:
1, for WinForm and ASP. NET interface design can do 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 codes,           //}//          or//           if (!this. DesignMode)          //{      }         }


Sometimes it is necessary to add some action to the constructor (although this is not advocated), the problem arises, 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 you still write it, you will get an error.       //if (String.Compare (). Process.getcurrentprocess (). ProcessName, "devenv") = = 0)       //         return;       }


ASP. NET can refer to the above wording
2, for WPF, you can refer to the above wording, there is another way of writing
if (! Designerproperties.getisindesignmode (this))     {         //other code    

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # Design interface, object references are not set to an object instance problem solution

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.