1、通過全局變量
2、通過構造函數傳遞參數
3、通過委托實現
通過委托例子:WebForm1 向Class1傳遞參數值:
//
{
? ? public partial class WebForm1 : System.Web.UI.Page
? ? {
? ? ? ? //step1.聲明一個委托
? ? ? ? public delegate void DoSomethingEventHandler(string s1);
? ? ? ? protected void Page_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //step2.通過委托調用其它類中的方法
? ? ? ? ? ? DoSomethingEventHandler myDelegate = new DoSomethingEventHandler(Class1.DoSomething);
? ? ? ? ? ? //step3.實現向其它類傳遞參數值
? ? ? ? ? ? myDelegate("去吧");
? ? ? ? }
? ? }
}
//
? ? public class Class1
? ? {
? ? ? ? private static string OperValue = string.Empty;
? ? ? ? //接受代理傳來參數的方法
? ? ? ? public static void DoSomething(string s1)
? ? ? ? {
? ? ? ? ? ? OperValue = s1;
? ? ? ? }
? ? }