How to use the same instance for several classes?

0

Below I'll try to give a value to the progressBar that is in the Controles class.

The method of Form1 , will call the method of class ManipularProgressBar to change the value of Control ProgressBar that is in class Controles .

But just ... when I get the progressBar control that is in the instance and assigns a value, it gives the following message: Object reference not set to an instance of an object.

This error occurs because I'm setting a new instance of an object, and it returns null . But just that, I do not know how best to solve this problem. I do not know exactly where to put the instance or if I need to instantiate.

I'm thinking of transforming the class ManipularProgressBar into static or creating an instance of it global so that all classes have access to the same value without reset.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Controles controls = new Controles();
        ManipularProgressBar mp = new ManipularProgressBar();

        controls.progressBar = this.progressBar1;            
        mp.editProgressBar();
    }
}

public class ManipularProgressBar
{  
    public void editProgressBar()
    {
        Controles controls = new Controles();
        controls.progressBar.Value = 50;
    }
}

public class Controles
{
    public ProgressBar progressBar { get; set; }
}
    
asked by anonymous 29.06.2018 / 01:38

2 answers

2

What makes the most sense there, is to pass the progressBar as a parameter in the edit method, because you are making the call to the edit without it knowing which progressBar will be edited.

    controls.progressBar = this.progressBar1;            
    mp.editProgressBar(controls.progressBar);
    
29.06.2018 / 13:21
-1

Dude, the problem is in the way you're abstracting things ... Looking specifically at this scenario, the Control class does not make sense to exist, because if you receive an instance of a ProgressBar from the Form, you could simply have the editProgressBar method receive the Progress instance and change the value. It would be something like this:

    private void Form1_Load(object sender, EventArgs e)
    {
        new Classe1().editProgressBar(this.progressBar1);            
    }    

public class Classe1
{
    public void editProgressBar(ProgressBar _ProgressBar, int valor = 50)
    {
        _ProgressBar.Value = valor;
    }
}

If for some reason, you really need the Control class to exist, then the approach needs to be a bit different.

private void Form1_Load(object sender, EventArgs e)
        {
            Controle _controle = new Controle();
            _controle._ProgressBar = this.progressBar1;
            new Classe1().editProgressBar(_controle._ProgressBar);            
        }

  public class Controle
    {
        public ProgressBar _ProgressBar { get; set; }
    }

In this second case, Class1 stays the same.

Obviously this is just a pseudo-code, but it caters to this scenario ...

    
29.06.2018 / 03:00