. NET Component Programming (10) supplement isupportinitialize

Source: Internet
Author: User

I didn't plan to write isupportinitialize, but I always thought the series was incomplete. I wrote this isupportinitialize article. In fact, the isupportinitialize interface is also very useful in component. So what is the purpose of isupportinitialize?
The description on msdn is as follows: specify that this object supports simple transaction processing notifications for batch initialization. Isupportinitialize allows controls to be optimized for multiple attributes. Therefore, you can initialize interdependent attributes or set multiple attributes in batches during design.
In isupportinitialize, there are two methods: begininit and endinit. the initialization of the ininit method is about to begin when the signal notification object is called, and the initialization of the endinit method is called.
Then how does isupportinitialize solve the dependency between attributes? See the following source code:

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. componentmodel;
Using system. Windows. forms;

Namespace classlibrary1
{
/**//*
Requirement: We need to execute the test1 method after class1 Initialization is complete.
So where do we need to write the test1 call?
Location 1: No, because when the class1 constructor is called, neither attribute a nor attribute B has been assigned a value.
Location 2: No. Although attribute a has been assigned a value, attribute B has no value.
Position 3: The row has been assigned values for attributes a and B.
Position 4: All attributes are assigned a value.

So why not write the test call directly on location 2 or 3? The main reason is: in the development process, programmers are not easy to know the attribute assignment first.
The post-order (that is, whether to assign values to A or B first when initializing class1), and position 4 can be sure that all attributes have been assigned values.
*/
Public class class1: component, isupportinitialize
{
Public class1 ()
{
// Location 1
}

Private string _;
Public String
{
Get {return _ ;}
Set
{
_ A = value;
// Location 2
}
}

Private string _ B;
Private string B
{
Get {return _ B ;}
Set {
_ B = value;
// Location 3
}
}

// Start component Initialization
Public void begininit ()
{

}

// Component completes Initialization
Public void endinit ()
{
_ C2. B (); // location 4
}

Public void test1 () // This method depends on attribute a and attribute B
{
MessageBox. Show (a + ":" + B );
}
}
}

If you have any questions, please leave a message or comment on me.

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.