I want to have an array of objects that is shared between two different methods.
The onclick for button1 on my webpage calls method1, which populates the needed values for the objectArray. I need the onclick method for button2 to be able to access the same objectArray with the same data that method1 was working with.
using myWebReference;
{
public partial class _Default : System.Web.UI.Page
{
ObjectArray[] myObjects = new ObjectArray[100];
public void Page_Load(object sender, EventArgs e)
{
//I have nothing in here at the moment
}
public void method1(object sender, EventArgs e)
{
//myObjects[]'s values are calculated and assigned here.
}
public void method2(object sender, EventArgs e)
{
String key = myObjects[0].value;
//when I try to get data within myObjects here, myObjects does exist,
//but it is empty and I get a null reference error when I try to use its values.
}
}
As @Peter Repac said, you could also use the ViewState. Like this:
Be wary of using this if
myObjectsgets quite large, as the ViewState is sent with the page, so bloating its size could impact performance. More info about ViewState here.