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.