1
votes

Here is my scenario:

I have a windows form designed in Visual Studio 2010. The form is quite simple. It has 2 text boxes with FirstName and LastName label. And one Display Button.

I have a class name Friend. This class has a Display() method that simply display the first and lastname in a MessageBox.

Now what I am trying to do is:

I instantiate an object "f1" of class Friend at form1_load(object sender, EventArgs e) method. and On the form Display button Click event, I tried to call the Display() method of friend class using f1 object that I instantiate on Form Load event.

But it gave me the error message " The name f1 doesn't exist in the current context."

Is it possible to do what I am trying to do anyway?

Thank You for your help.

1

1 Answers

4
votes

Just make it a member variable rather than local:

public class MyForm : Form 
{
     Friend f1;

    private void OnLoad()
    {
       f1 = new Friend();
    }

    private void Display()
    {
       // use f1 here
    }

}