I am trying to declare a global array that can be visible and accessed (ie. populated and accessed) from different functions, all within the same class.
I have declared it like so:
public class MyClass: MyParentClass
{
private string[] stringArray = new string [3];
private void Page_Load(object sender, System.EventArgs e)
{
(Other code here)
I'm just putting in the line 'private void Page_Load(object sender, System.EventArgs e)' so you have an idea of where in my code the array is being declared.
I am then trying to populate it in another function within the MyClass class, as follows:
protected void button_onclick(object sender, System.Web.UI.ImageClickEventArgs e)
{
(Other code here)
stringArray = strText.Split('|');
(Other code here)
}
This works.
And then I am trying to access each item in the array in another further function:
protected void button2_onclick(object sender, System.Web.UI.ImageClickEventArgs e)
{
(Other code here)
if (strText2 == stringArray[0]) (I have also tried putting .ToString after the second ']')
(Other code here)
}
When I run this I get the message 'Object reference not set to an instance of an object'. One of the trouble shooting tips is to use the 'new' keyword to create an object instance. However, I have already used that when declaring the array, and consequently that array is visible in the fuction where it is populated. So why is it not visible in this last function (button2_onclick)?
All the other code is fine, I've been stepping through it.
Thanks.

New Topic/Question
Reply



MultiQuote




|