How to use the same instance for multiple classes?

Asked

Viewed 116 times

0

Below I will try to give a value to the progressiBar that is in the class Controles.

The method of Form1, will call the class method ManipularProgressBar to change the Control value ProgressBar who’s in class Controles.

But only that. when I get the progression controlBar which is in the instance and I assign a value, gives the following message: object reference not defined for an instance of an object.

This error occurs because I am defining a new instance of an object, there it returns null. But just that, I don’t know how to best solve this problem. I don’t know exactly where to put the instance or if I need to instantiate.

I’m thinking of turning the class ManipularProgressBar static or create a global instance of it so that all classes have access to the same value without resetting.

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

    Better to say what you want because this code doesn’t seem to make the slightest sense and create a static class just seems to increase the common sense. The names of everything are too bad, no idea.

2 answers

2


What makes more sense there, is you pass the progressBar as parameter in the method of Edit, because you this farm the call of Edit without it knowing which progressBar will be edited.

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

-1

Dude, the problem is how you’re abstracting things... Looking specifically at this scenario, the Control class makes no sense to exist, because if you receive an instance of a Form Progressibar, you could simply make the editProgression methodBar received the Progress instance and made the value change. 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 little 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, Classe1 is the same.

Obviously this is just a pseudo-code, but it fits that scenario...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.