如何将值从一个子类传递到主类C#中的另一个子类?

时间:2017-02-26 05:42:11

标签: c# class subclass

我创建了一个名为 MyMainClass

的类
namespace MyTest
{
        private void frmMain_Load(object sender, EventArgs e)
        {
            MyMainClass myVar = new MyMainClass();

            myVar.sub1.sb1A = "value 1A";
            myVar.sub1.sb1B = "value 1B";

            MessageBox.Show(myVar.sub2.wantPassString);
            //I want to print the value "I've got value value 1A"
         }            
    public class MyMainClass
    {
        public subClass1 sub1 = new subClass1();
        public subClass2 sub2 = new subClass2();

        public class subClass1
        {
            public string sb1A{get;set;}
            public string sb1B{get;set;}
        }
        public class subClass2
        {
            public string sb2A{get;set;}
            public string sb2B{get;set;}
            // a1a is a value that I want to get from subClass1
            string a1a = subClass1.sb1A;

            public string wantPassString {get{return "I've got value " + a1a;}}
        }
    }
}

如何将 subClass1.sb1A 中的值传递给subClass2中的字符串 a1a wantPassString ?当我打电话给frmMain_Load

4 个答案:

答案 0 :(得分:1)

通常的方法是通过构造函数传递类。这有效:

public class MyMainClass
{
    public MyMainClass()
    {
        sub1 = new subClass1();
        sub2 = new subClass2(sub1);
    }
    public subClass1 sub1;
    public subClass2 sub2;

    public class subClass1
    {
        public string sb1A{get;set;}
        public string sb1B{get;set;}
    }
    public class subClass2
    {
        public subClass2(subClass1 sub1)
        {
            this.sub1 = sub1;
        }
        public string sb2A{get;set;}
        public string sb2B{get;set;}
        subClass1 sub1;

        public string wantPassString {get{return "I've got value " + sub1.sb1A;}}
    }
}

答案 1 :(得分:0)

一个选项是创建方法而不是像

这样的属性
     public string wantPassString(string param)
     {
         return "I've got value " + param;
     }

并在frmMain_Load中调用此方法

 MessageBox.Show(myVar.sub2.wantPassString(a1a));

答案 2 :(得分:0)

创建第三个类来保存数据。

public MainClass
{
    private class sharedClass
    {
       internal string sb1A { get; set; }
    }

    public class subClass1
    {

        private readonly sharedClass _shared;
        internal subClass1(sharedClass shared)
        {
            this._shared = shared;
        } 

        public string sb1A
        {
            get
            {
               return this._shared.sb1A;
            }
            set
            {
                this._shared.sb1A = value;
            }

        public string sb1B{get;set;}
    }

    public class subClass2
    {
        private readonly sharedClass _shared;
        public subClass2(shared s) 
        { 
            _shared = s;
        }
        public string sb2A{get;set;}
        public string sb2B{get;set;}


        public string wantPassString {get{return "I've got value " + _shared.sb1;}}
    }

    private readonly sharedClass _shared = new sharedClass();
    private readonly subClass1 _subClass1;
    private readonly subClass2 _subClass2;

    public MainClass() 
    {
        this._subClass1 = new subClass1(this._shared);
        this._subClass2 = new subClass2(this._shared);
    }
}

答案 3 :(得分:0)

您可以添加其他属性和方法,并将其作为参数传递:

 public string sb2C{ get; set; }
        public string getData(string strParam)
        {
            return sb2C= strParam;
        }

然后在你的frmMain_Load:

MessageBox.Show(myVar.sub2.getData(myVar.sub1.sb1B));
//This would also populate sb2C that will hold the data from the other class