如何设置class-property并在另一个类上重用它?

时间:2016-05-10 21:44:26

标签: c# asp.net

我有一个带输入的类,我通过类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?

2 个答案:

答案 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