4 Replies - 5490 Views - Last Post: 20 September 2011 - 04:17 AM Rate Topic: -----

#1 c#Newbie1978  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-September 11

'Global' array - not visible in other functions within same cl

Posted 20 September 2011 - 02:01 AM

Hi,

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.

Is This A Good Question/Topic? 0
  • +

Replies To: 'Global' array - not visible in other functions within same cl

#2 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: 'Global' array - not visible in other functions within same cl

Posted 20 September 2011 - 02:11 AM

My guess is that it is the element at index 0 that is null. Are you sure that the line
stringArray = strText.Split('|');


is in fact splitting the string into array elements? Put a break point in your code in the button2 click event and check the contents of the array. Also check the array when you are setting it (on the split function) to make sure it has some values in it.
Was This Post Helpful? 1
  • +
  • -

#3 c#Newbie1978  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-September 11

Re: 'Global' array - not visible in other functions within same cl

Posted 20 September 2011 - 02:17 AM

Thanks for that.

If you see my first post, I mentioned that the string split works, as in, I've put a breakpoint in the button click event and can see the expected values in the array once the split function is executed. When I try to access stringArray in the button2 click event, all the array elements are null.
Was This Post Helpful? 0
  • +
  • -

#4 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: 'Global' array - not visible in other functions within same cl

Posted 20 September 2011 - 04:07 AM

It may help if we can see the full code for the class MyClass and your form with the buttons on it. Can you either post it or zip it up and attach it?

If not I have one suggestion. Create a accessor for the array like this:
public String[] StringArray
{
    get { return this.stringArray; }
    private set
    {
        // set value - put a break point here so that you can see whenever it is set
        this.stringArray = value;
    }
}

private string[] stringArray = new string[3];


Now change all your references to stringArray to use the accessor StringArray. Now put a break point on the line indicated, and you will be able to see whenever the array is set. When the breakpoint is triggered use the call stack to see which method is setting the array. This will help you track down if the array is being set at any point that you have overlooked.

This post has been edited by ragingben: 20 September 2011 - 04:07 AM

Was This Post Helpful? 1
  • +
  • -

#5 c#Newbie1978  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-September 11

Re: 'Global' array - not visible in other functions within same cl

Posted 20 September 2011 - 04:17 AM

Thanks for that, however I realised that as I'm using ASP.Net, that the array values weren't ebing saved because of post backs. So I've done it another way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1