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

0

Hello, I know it's a beast question, but I'm having trouble accessing a variable from another class. Situation: - I am using a datagrid, which I want to get the value of a 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 that value. But when I try to access the value of another class I can not.

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 appears Messagebox with a value of zero. I've tried this in several ways, but I could not get it to work.

Location:

I'm trying to put together a simple customer registration schedule, with phone address address these things. In Form2 is the screen where you have a Datagrid, with the information, of the database. I'm trying to create a button, to change the client data, I click on the record I want, when I click it it gets the registry ID, I click the change button and it opens Form1 already in the record that was selected in Form2. >

Thank you.

    
asked by anonymous 27.12.2017 / 18:33

2 answers

0

try to encapsulate is less susceptible 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 this way:

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);
    }
    
27.12.2017 / 19:47
0

Well try to create an instance control class, you're probably calling the click on a different instance of the click location

public static class ControleInstancia 
{
    public static Form2 Fomulario2;
}

Refer to the entire instance of the Form2 class in this row and then do the following to verify 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 the instance control class.

    
24.09.2018 / 13:41