Accessing variable in another class comes null - C# Visual Studio 2015

Asked

Viewed 3,047 times

1

Hello, I know it’s a stupid question, but I’m having a hard time accessing a variable from another class. Situation: - I’m using a datagrid, which I want to take the value of one column, and transport that value to another class.

Script:

public partial class Form2 : Form
{
    public string idString;
    public int idselecionada;

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {           
        idString = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
        idselecionada = Int32.Parse(idString);
        MessageBox.Show("ID " + idselecionada);
    }

This script works perfectly. I click on the line it takes the value of the first column, and shows me this value. But when I try to access the value of another class I can’t.

public partial class Form1 : Form
{
    private int idinicial;
    Form2 varivavel2 = new Form2();

private void Form1_Load(object sender, EventArgs e)
    {          
        idinicial = varivavel2.idselecionada;
        MessageBox.Show("ID Inicial " + idinicial);
    }

The problem is that when I open Form 1, it shows Messagebox with a value at zero. I’ve tried this many ways, but I couldn’t make it work.

Situation:

I’m trying to set up a simple customer registration schedule with phone name address these things. In Form2 is the screen where you have a Datagrid, with the information, of the database. I am trying to create a button, to change the customer data, I click on the record I want, when clicking is taken the registration ID, click on the change button and open the Form1 already in the record that was selected in Form2.

Since thanks.

  • Why do you turn something into string and then in int?

  • I’m kind of Noob, I kind of got the script on the net. If I take the Tostring, will it be full ovalor? or will it be as an object?

3 answers

1

tries to encapsulate gets less prone to errors

    form2:

private string idString;

{

get{ return idString; }

set{ idString = value; }

}
public int idselecionada;

{

get{ return idselecionada; }

set{ idselecionada = value; }

}

ai in form 1 you would use so :

public partial class Form1 : Form
{
    private int idinicial;
    Form2 varivavel2 = new Form2();

private void Form1_Load(object sender, EventArgs e)
    {          

        MessageBox.Show("ID Inicial " + variavel2.idinicial);
    }
  • I’ll try and let you know if it worked.

  • I tried that way but it didn’t work. It keeps presenting null in the same way.

1

In Form1, on the line Form2 varivavel2 = new Form2(); you are generating a new instance of Form2, and consequently the variable idselecionada has not yet been set.

An alternative is to create a constructor on Form2 who receives a Form as a parameter:

public partial class Form2 : Form
{
    Form _formularioPai = null;

    public Form2(Form formulario)
    {
        _formularioPai = formulario;
    }
}

Then at the event dataGridView1_CellClick could directly assign the selected value to a variable of the Form1.

1

Well try to operate an instance controller class, you are probably calling the click at a different instance than the click location

public static class ControleInstancia 
{
    public static Form2 Fomulario2;
}

Mention the whole instance of the Form2 class in this vairavel do the following to see if it is already in memory. You probably start the view from somewhere in your code

if(ControleInstancia.Fomulario2 == null){
    ControleInstancia.Fomulario2 = new  Form2();
}

Now on your Form1

public partial class Form1 : Form
{
    private int idinicial;

    private void Form1_Load(object sender, EventArgs e)
    {          

        MessageBox.Show("ID Inicial " + ControleInstancia.Fomulario2.idinicial);
    }
}

Remember to start your form2; And always call for instance control class.

Browser other questions tagged

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