C # several methods for passing values between windows (classes) in the middle!

Source: Internet
Author: User

Author:PeterXuSource: Blog. CSDN Blog: http://blog.csdn.net/peterreg/
Copyright Notice: Original works can be reproduced. During reprinting, you must mark the original Publishing, author information, and this statement in the form of hyperlinks. Otherwise, legal liability will be held. Address: http://blog.csdn.net/peterreg/archive/2008/04/08/2259404.aspx

The transfer between classes plays an important role in our daily programming. Various implementation methods have a great impact on program scalability and performance.

For example, if the object of Class A is an instance of Class B, object B needs to access an attribute A1 of object.
Class
{
Private int m_iA1;
Public int A1
{
Get {return this. m_iA1 ;}
Set {this. m_iA1 = value ;}
}
}

1. Pass object A as A parameter to Class B, that is, pass object A to Class B in the form of an attribute or constructor, and then call A1 through reference of object A in object B.
Class B
{
Private A m_oA;
Public B (A)
{
This. m_oA =;
}
}

Class B
{
Private A m_oA;
Public B ()
{
}
Public
{
Set {this. m_oA = value ;}
}
}
 Disadvantages: Class A and Class B have A high Coupling Degree. Once other classes think of the function of Class A, Class A needs to be extended.
Improvement:A unified abstraction is designed for Class A. The interface or abstract class mode can reduce coupling in A certain program.
For more information about interfaces and abstract classes, see my previous article"

2. Use the format of intermediate files, such as XML, text files, and databases.
Disadvantages:Inefficient, unified file format, and packaging and Package
Advantages:It can transmit a large amount of information.

3. Shared memory space
Disadvantages:There is a certain degree of complexity, there are some reference code on the Internet

4. Attributes of Class A adopt Static mode Static
Disadvantages:This method is invalid when some attributes cannot be set as static attributes.

5. Proxy
Disadvantages:Efficiency is slightly lower, with little impact
Advantages:Good program encapsulation and scalability



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.