C # example (2)-use delegate to pass values

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace DelegateTest2
{
Public delegate void tranDelegate (string test );
Class Program
{
// Value transfer between classes, which can be used to return a value to the parent form by a child form
Static void Main (string [] args)
{
ParentClass parent = new ParentClass ();
ChildClass child = new ChildClass (parent. _ myDele );
// The value of the parent form is simple to pass to the child form
Child. Result = parent. Test;
Console. WriteLine ("value obtained from the parent form: {0}", child. Result );
// Return the child form to the parent form
Child. sendValue ();
Console. WriteLine ("value obtained from the subform: {0}", parent. Result );
Console. ReadLine ();
}
}
Class ParentClass
{
Private string _ test = "parent ";
Private static string _ result = "";
Public tranDelegate _ myDele = delegate (string test)
{
_ Result = test;
};
Public string Result
{
Set {_ result = value ;}
Get {return _ result ;}
}
Public string Test
{
Set {this. _ test = value ;}
Get {return this. _ test ;}
}

}
Class ChildClass
{
Private string _ test = "child ";
Private string _ result = "";
Private tranDelegate _ tranDele;
Public ChildClass (tranDelegate tranDele)
{
_ TranDele = tranDele;
}
Public string Test
{
Get {return _ test ;}
Set {_ test = value ;}
}
Public string Result
{
Set {this. _ result = value ;}
Get {return this. _ result ;}
}
Public void sendValue ()
{
If (_ tranDele! = Null)
{
_ TranDele (this. _ test );
}
}
}
}
It simulates the subforms used in Windows applications to delegate the return value to the parent form. Of course, it is better to write events to the subforms.

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.