using a public variable in different methods within the same class

i know i am fogetting something simple here.  i am trying to assign values to a boolean array and change them in a seperate method. I am getting the error " Enable[0] error: 'Enable' is not an array or pointer. Indexing is not available "

 I have:

public class MainForm : System.Windows.Forms.Form

{

   public  bool[] Enable;

   private void CobblePlots_Load(object sender, System.EventArgs e)

   {

      bool[] Enable = new bool[10]{false,false,false,false,false,false,false,false,false,false};

   }

 

   public void receiveMethod()

   {

      Enable[0] = ChosenGraphs.Items.Contains(penName);

   }




Answer this question

using a public variable in different methods within the same class

  • Rajavanya

    i think yout main issue is the declaration, why did u declare the Enable boolean type twice.

    once as global one as local in CobblePlots_Load.

    if u declare twice that way the compiler will only recognize the global one which is not assigned with any value.

    That why the error say Indexing not available.

    try take away the bool[ ] in the cobblePlots_Load.

    private void CobblePlots_Load(object sender, System.EventArgs e)

    {

    Enable = new bool[10]{false,false,false,false,false,false,false,false,false,false};

    }


  • Jack.NET

    How about doing like this

    public bool[] Enable = new bool[10] { false, false, false, false, false, false, false, false, false, false };

    Take it out of your load method and have it as it's own property.

     

    public class MainForm : System.Windows.Forms.Form

    {

       public bool[] Enable = new bool[10]
         {
    false, false, false, false, false, false, false, false, false, false };

       private void CobblePlots_Load(object sender, System.EventArgs e)

       {

       }

     

       public void receiveMethod()

       {

          Enable[0] = ChosenGraphs.Items.Contains(penName);

       }



  • seamonkeyz

    Try this:

    public class MainForm : System.Windows.Forms.Form

    {

    public bool[] Enable = new bool[10]{false,false,false,false,false,false,false,false,false,false};;

    public void receiveMethod()

    {

    Enable[0] = ChosenGraphs.Items.Contains(penName);

    }

    or

    public class MainForm : System.Windows.Forms.Form

    {

    public bool[] Enable;

    public MainForm()

    {

    this.Enable = new bool[10]{false,false,false,false,false,false,false,false,false,false};

    }

    public void receiveMethod()

    {

    Enable[0] = ChosenGraphs.Items.Contains(penName);

    }

    You were declaring Enable twice.

    I hope these will work fine.

    Best Regards,

    Rizwan



  • Manuel Sampen

    thanks guys, I didnt even realize i was declaring it twice.

  • using a public variable in different methods within the same class