简体   繁体   English

在C#中创建一个类对象

[英]Creating a class object in C#

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: 我正在尝试创建一个类的对象,但它似乎不起作用,我不禁想到我从JAVA的角度来看这个:

 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 使用PortCheckObject调用我的方法MyMethod时,我会出错

(PortChecks is the class name) (PortChecks是班级名称)

It's because it's outside of the scope of testCheck_Click 这是因为它超出了testCheck_Click的范围

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

    public Form1()
    {
        InitializeComponent();
    }

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

PortChecks PortCheckObject in Form1 constructor is a local variable. Form1构造函数中的PortChecks PortCheckObject是一个局部变量。
Put its declaration as a private field in Form1 class. 将其声明作为Form1类中的私有字段。

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

    public Form1()
    {
        InitializeComponent();
    }

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

@James, @詹姆士,

You need a class property with the name 'PortCheckObject' and can be possible to access in other parts of the class. 您需要一个名为“PortCheckObject”的类属性,并且可以在该类的其他部分中访问。

public partial class Form1 : Form
{
    private PortChecks PortCheckObject;

    public Form1()
    {
        InitializeComponent();

        PortCheckObject = new PortChecks();
    }

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

This is a general scope issue, not a Java vs C# issue (as your code wouldn't work in Java either). 这是一个通用的范围问题,而不是Java与C#问题(因为您的代码也不适用于Java)。 PortCheckObject is in Form1()'s scope, not testCheck_Click's scope. PortCheckObject在Form1()的范围内,而不是testCheck_Click的范围。 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();
    }

This is an instance of a scope problem. 这是范围问题的一个实例。 You do not have scope in your testCheck_Click method. 您的testCheck_Click方法中没有范围。 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();
    }

...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM