Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing value across methods

Status
Not open for further replies.

yonelay

Programmer
Jan 17, 2007
10
US
namespace Sample1

{

public partial class MainForm : Form

{

private int field1;

public MainForm()

{

InitializeComponent();

field1 = 2;

}

private void button1_Click(object sender, EventArgs e)

{

if (field1 == 2)

{

do something here;

}

else

do other;

}



How can I get the field1 value to be 2 when button1_Click method is called? Right now everytime I call field1, I get null value. I'd like to recall the value that is assigned in one method to another method. Thanks for your answer.


 
I don't see anything wrong with that code with the exception of some missing } at the end...

There must be something else that is throwing the null reference exception...
 
It is impossible for field1 to be null as it is an int and therefore the compiler will automatically give it a zero value?.... As JurkMonkey said, something else is wrong here. Could you paste the code that is calling it?

Age is a consequence of experience
 
As an added note, if it was declared an int? (with the questionmark) then it could be null.

? <- this makes any primitive data type nullable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top