1

I have listBox1 but when I am using that listBox1 inside the buttonClick I can access but outside the buttonClick I can't access . Where I am doing mistakes ? Thanks

namespace design
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(button1.Text);// I can access listBox1 here...
        }

        listBox1.//I can't access listBox1 here....
    }
}
Michel Ayres
  • 5,891
  • 10
  • 63
  • 97

2 Answers2

12

You can access it there but it's not working because you are not in any function or method.

You can't just start typing code somewhere in a class, you need to handle some kind of event or something.

This is very basic C# knowledge btw.

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
  • I can't see listBox1 there.And everytime am I have to use in function or method?why ? –  Jul 18 '12 at 13:27
  • How else would the compiler know when to execute your code? C# is an event-driven language. I suggest you find some tutorials on the internet or a good book to study. – Gerald Versluis Jul 18 '12 at 13:28
  • Ok now understood that I have use in function or method.you said you can access but I can't see with intellisense. –  Jul 18 '12 at 13:37
  • 1
    If you are in a place where it is valid to access the listbox you can see it in IntelliSense – Gerald Versluis Jul 18 '12 at 13:38
1

Your code is wrong. You need to put listBox1 inside some method to access it.

namespace design
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(button1.Text); // This part is inside a event click of a button. This is why you can access this.
        }

        public void accessList()
        {
            listBox1.Items.Add(button1.Text); // You'll be able to access it here. Because you are inside a method.
        }
        // listBox1. // you'll NEVER access something like this. in this place
    }
}

Maybe you wanna do a property ?

Michel Ayres
  • 5,891
  • 10
  • 63
  • 97
  • What do you mean with "do a property" ?What can I do with it ? –  Jul 18 '12 at 13:50
  • `A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.` [Source](http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx). Also take a look at [C# - When to use properties instead of functions](http://stackoverflow.com/questions/1374273/c-sharp-when-to-use-properties-instead-of-functions) – Michel Ayres Jul 18 '12 at 13:52
  • No problem. We try to help everyone, so they could help us in future! ^_^ – Michel Ayres Jul 18 '12 at 13:55