您的位置:首页 > 编程语言 > C#

C# 两个类之间传递数据

2018-02-22 16:47 162 查看
1、通过全局变量2、通过构造函数传递参数3、通过委托实现通过委托例子:WebForm1 向Class1传递参数值://////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace WebApplication2
{
    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#