2

I am trying to create an object of a class, but it doesn't seem to work, I can't help but think I am looking at this from a JAVA perspective:

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            PortChecks PortCheckObject = new PortChecks();

        }

private void testCheck_Click(object sender, EventArgs e)
        {
            PortCheckObject.MyMethod();
        }

I can error when using the PortCheckObject to call my method MyMethod

(PortChecks is the class name)

2
  • 3
    That wouldn't work in Java either. Commented Sep 12, 2011 at 19:55
  • Your PortCheckObject is a local variable in the contructor of Form1 that you cannot access from testCheck_Click. This would be the same even in Java. Commented Sep 12, 2011 at 19:56

5 Answers 5

9

It's because it's outside of the scope of testCheck_Click

public partial class Form1 : Form
{
    PortChecks PortCheckObject = new PortChecks();

    public Form1()
    {
        InitializeComponent();
    }

    private void testCheck_Click(object sender, EventArgs e)
    {
        PortCheckObject.MyMethod();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, should have seen thar,, ^ ^
3

PortChecks PortCheckObject in Form1 constructor is a local variable.
Put its declaration as a private field in Form1 class.

public partial class Form1 : Form
{
    private PortChecks PortCheckObject = new PortChecks();

    public Form1()
    {
        InitializeComponent();
    }

    private void testCheck_Click(object sender, EventArgs e)
    {
        PortCheckObject.MyMethod();
    }
}

Comments

2

@James,

You need a class property with the name 'PortCheckObject' and can be possible to access in other parts of the class.

public partial class Form1 : Form
{
    private PortChecks PortCheckObject;

    public Form1()
    {
        InitializeComponent();

        PortCheckObject = new PortChecks();
    }

    private void testCheck_Click(object sender, EventArgs e)
    {
        PortCheckObject.MyMethod();
    }
}

Comments

1

This is a general scope issue, not a Java v.s. C# issue (as your code wouldn't work in Java either). PortCheckObject is in Form1()'s scope, not testCheck_Click's scope. Try the following:

public partial class Form1 : Form
{
    private PortChecks PortCheckObject;

    public Form1()
    {
        InitializeComponent();

        PortCheckObject = new PortChecks();

    }

private void testCheck_Click(object sender, EventArgs e)
    {
        PortCheckObject.MyMethod();
    }

Comments

0

This is an instance of a scope problem. You do not have scope in your testCheck_Click method. Make the following change and it should work:

public partial class Form1 : Form
{
    private PortChecks MyPortCheck {get; set;}

    public Form1()
    {
        InitializeComponent();
        MyPortCheck = new PortChecks();
    }

    private void testCheck_Click(object sender, EventArgs e)
    {
        MyPortCheck .MyMethod();
    }

...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.