我有一个带输入的类,我通过类A设置值。如何在类B中访问这些属性值?
E.g。
namespace Example{
public class Inputs {
public string Something { get; set; }
}
A类:
Inputs test = new Inputs();
test.Something = txtSomething.Text;
B组:
//How do I access values I declared in class A, or did I do something wrong?
答案 0 :(得分:0)
public class B{
public Inputs Test { get; set; }
}
var b = new B();
b.Test = new Inputs();
b.Test.Something = txtSomething.Text;
public class B{
public B(Inputs myB)
{ this.MyB = myB; }
public Inputs MyB { get; set; }
}
Inputs test = new Inputs();
test.Something = txtSomething.Text;
var b = new B(test );
答案 1 :(得分:0)
只需在其构造函数中为B
提供test
的副本。
public class A
{
private Inputs _test;
public A()
{
_test = new Inputs();
new B(_test);
}
}
public class B
{
private Inputs _input;
public B(Inputs input)
{
_input= input;
}
}
A._test.Somthing
的任何更改也会显示在B._input.Somthing
。