【问题标题】:Access from my main form class into specific class method从我的主表单类访问特定的类方法
【发布时间】:2013-06-04 09:38:17
【问题描述】:

这是我的主要表单类,里面有Stop button click event

public partial class MainWin : Form
{
    private Job job = new...

    private void btnStop_Click(object sender, EventArgs e)
    {
       job.state = true;
    }
}

当我点击停止按钮时,我将我的作业类成员从 false 更改为 true,我想要做的是当此变量更改为 true 时,我想访问作业类中的特定方法并做一些事情。

public class Job
{
    public bool state { get; set; }

    private void processFile() // i want access to this method in order to change other class state
    {
       // do work
    }
}

我该怎么做?

【问题讨论】:

  • 您不能访问类之外的私有方法。是否允许将方法的访问说明符更改为 internal/public?

标签: c# winforms


【解决方案1】:

真的很难说出你的确切意思,但是在设置属性时调用方法的一种方法是扩展 auto 属性并做到这一点。

public class Job
{
    private bool state;
    public bool State
    {
       get { return this.state; }
       set
       {
          this.state = value;
          processFile();
       } 

    private void processFile()
    {
       // do work
    }
}

但是,只是猜测和看到这一点代码,您可能想要重新设计您的工作方式。

【讨论】:

  • 如果将State设置为false,会再次处理。
【解决方案2】:

如果真的不想暴露你的私有方法,你可以这样做:

public class Job
{
    private bool state;

    public bool State
    {
        get
        {
            return state;
        }
        set
        {
            if (state != value)
            {
                state = value;
                OnStateChanged();
            }
        }
    }

    private void OnStateChanged()
    {
        if (state) // or you could use enum for state
            Run();
        else
            Stop();
    }

    private void Run()
    {
        // run
    }

    private void Stop()
    {
        // stop
    }
}

但是您真的应该考虑创建公共 Job.Run 方法并将 Job.State 保留为只读。如果你想让对象执行一些操作,方法会更适合这个。

【讨论】:

    【解决方案3】:

    像这样创建Job 类:

    public class Job
    {
        private bool _isRunning = false;
        public bool IsRunning { get { return _isRunning; } }
    
        public void StartProcessing()
        {
            if (_isRunning)
            {   
                // TODO: warn?      
                return;
            }
            ProcessFile();
        }
    
        public void StopProcessing()
        {
            if (!_isRunning)
            {   
                // TODO: warn?
                return;
            }
            // TODO: stop processing
        }
    
        private void ProcessFile()
        {
            _isRunning = true;
    
            // do your thing
    
            _isRunning = false;
        }
    }
    

    然后像这样消费它:

    public partial class MainWin : For
    {
        private Job _job = new Job();
    
        private void StartButton_Click(object sender, EventArgs e)
        {
            if(!_job.IsRunning)
            {
                _job.StartProcessing();
            }
        }
    
        private void StopButton_Click(object sender, EventArgs e)
        {
            if(_job.IsRunning)
            {           
                _job.StopProcessing();
            }
        }
    }
    

    线程安全作为练习被忽略了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-16
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 2013-03-24
      • 1970-01-01
      • 2019-09-09
      相关资源
      最近更新 更多