C# 两个类之间传递数据
生活随笔
收集整理的這篇文章主要介紹了
C# 两个类之间传递数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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;
? ? ? ? }
? ? }
總結
以上是生活随笔為你收集整理的C# 两个类之间传递数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VIVO X5M手机ROOT权限获取方法
- 下一篇: iOS关于armv7,armv7s,ar