在Windows窗体中获取值到另一个类?

时间:2017-07-19 09:13:33

标签: c# winforms oop

Windows窗体上的代码是

    private void button2_Click(object sender, EventArgs e)
    {

            CandleCollection collection = GetCandleCollection();
            int Dim = int.Parse(txt_agent.Text);
            int NumParticles = int.Parse(txt_part.Text);

            SOSManager p = new SOSManager(collection);

          //this part 
            p.Dim = Dim;
            p.NumParticles = NumParticles;

            m_part = new ParticleSwarm(fit,p.Dim, p.NumParticles);



    }

所以,我想在文本框中添加值给这个类。

public class SOSManager
{        
    private ParticleSwarm m_part;
    public ParticleSwarm BackTestPartReport
    {
        get
        {
            return m_part;
        }
    }

我声明了这个

    public int Dim; //this part 
    public int NumParticles;

    public  double fit;

增加价值。

    public SOSManager(CandleCollection collection)
    {
        CandleList = collection;            
        Calculate();

        m_backTesting = new BackTesting(this);
        fit = m_backTesting.fitness;

        //this part
        m_part = new ParticleSwarm(fit, Dim, NumParticles);
        m_part.Calculate(Dim,NumParticles);
        //

    }

现在,我无法从Windows获得这个类的价值。我该怎么办?

1 个答案:

答案 0 :(得分:0)

不要在太晚时设置属性,而是将它们传递给具有更多参数的构造函数:

public SOSManager(CandleCollection collection, int Dim, int NumParticles)
{
    // If you still need to store them as properties:
    this.Dim = Dim;
    this.NumParticles = NumParticles;

然后这样称呼:

SOSManager p = new SOSManager(collection, Dim, NumParticles);