user2214609
user2214609

Reputation: 4961

Access from my main form class into specific class method

This is my main form class and inside i have Stop button click event:

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

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

When my stop button clicked i change my job class member from false to true and what i want to do is when this variable changed to true i want to access to specific method inside job class and do something.

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
    }
}

how can i do it ?

Upvotes: 0

Views: 106

Answers (3)

CodeCaster
CodeCaster

Reputation: 151604

Create the Job class like this:

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;
    }
}

Then consume it like this:

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();
        }
    }
}

Thread safety left out as exercise.

Upvotes: 0

OpenMinded
OpenMinded

Reputation: 486

If really don't want to expose you private method, you can do something like this:

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
    }
}

But you should really consider creating public Job.Run method and leaving Job.State readonly. If you want the object to perform some operations, the methods will be more suitable for this.

Upvotes: 0

Zach Johnson
Zach Johnson

Reputation: 24232

It's really hard to tell what you exactly mean, but one way to invoke a method when the property is set would be to expand the auto property out and do exactly that.

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

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

However, just guessing and seeing this little bit of code, you might want to redesign how you're doing things.

Upvotes: 2

Related Questions