课堂上的错误&访问非静态成员 [英] Error with class & accessing non-static members

查看:56
本文介绍了课堂上的错误&访问非静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我试图理解为什么我无法在当前代码中访问我的文本框。



这是执行程序的代码(Windows窗体):



Hi,

Im trying to understand why i can't access my textbox in my current code.

This is the code to execute the program (Windows Form):

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

        class Node
        {
            public Node prev, next; // to store the links
            public object data; // to store the data in a node
            public Node()
            {
                this.prev = null;
                this.next = null;
            }
            public Node(object data)
            {
                this.data = data;
                this.prev = null;
                this.next = null;
            }
            public Node(object data, Node prev)
            {
                this.data = data;
                this.prev = prev;
                this.next = null;
            }
            public Node(object data, Node prev, Node next)
            {
                this.data = data;
                this.prev = prev;
                this.next = next;
            }
        }
        class DoublyLinkedList
        {
            public Node head, current;
            public void AddNode(object n) // add a new node
            {
                if (head == null)
                {
                    head = new Node(n); //head is pointed to the 1st node in list
                    current = head;
                }
                else
                {
                    while (current.next != null)
                    {
                        current = current.next;
                    }
                    current.next = new Node(n, current); //current is pointed to the newly added node
                }
            }

            public Node Find(object n) // finds a node in the DLL
            {
                current = head;
                while ((current != null) && (current.data != n))
                    current = current.next;

                if (current == null)
                    return null;
                else
                    return current;
            }

            public String Remove(object n)
            {
                String Output = "";
                if (head == null)
                {
                    Output += "\r\nLink list is empty";
                }
                else
                {
                    current = Find(n);
                    if (current == null)
                    {
                        Output += "\r\nNode not found in Doubly Linked List";
                    }
                    else
                    {
                        current.next.prev = current.prev;
                        current.prev.next = current.next;
                        current.prev = null;
                        current.next = null;
                        Output += "\r\nNode removed from Doubly Linked List\r\n";
                    }
                }
                return Output;
            }

            public String PrintNode() // print nodes
            {
                String Output = "";
                Node printNode = head;
                if (printNode != null)
                {
                    while (printNode != null)
                    {
                        Output += printNode.data.ToString() + "\r\n";
                        printNode = printNode.next;
                    }
                }
                else
                {
                    Output += "No items in Doubly Linked List";
                }
                return Output;
            }

            private void btnExecute_Click(object sender, EventArgs e)
            {
                DoublyLinkedList dll = new DoublyLinkedList();
                //add new nodes
                dll.AddNode("Tom");
                dll.AddNode("Jack");
                dll.AddNode("Mary");
                dll.AddNode("Meir");
                dll.AddNode("Toxic");
                Find("Meir");
                Remove("Toxic");
                //print nodes
                txtOutput.Text = dll.PrintNode();
            }
        }
    }
}





我确定有一个简单的方法解决它,希望有人能向我解释一下!谢谢



Im sure there is an easy way to fix it, hope someone can explain this to me! Thanks

推荐答案

发生的事情非常简单;请记住,在编译时,没有实例:您的表格;其控制;或者在其中定义的类(嵌套类)。



而且,你的内部类与Form1没有继承关系,可以让它们访问它的内容,比如控件在表格上。 Class在另一个Form或Class中定义的事实并不意味着它有一个'Parent。



编译器尝试创建'DoublyLinkedList类,并查找没有可用的'txtOutput TextBox实例的引用,所以它会产生你看到的错误。



即使你的定义一个EventHandler您的嵌套类不会导致编译错误:您无法在设计时为'btnExecute的Click事件设置EventHandler,因为它在属性浏览器中不可见。你必须在代码中设置EventHandler,你不需要这样做。



修复很简单:移动Click EventHandler的代码' btn执行所以它在Form1中,而不在Class'DoublyLinkedLists中。



在你这样做之后,你需要做一些其他的调整:
What's happening is very simple; keep in mind that at compile-time there are no instances of either: your Form; its Controls; or the Classes defined within it (nested Classes).

And, your internal Classes have no inheritance relationship with Form1 that would give them access to its contents, like the Controls on the Form. The fact a Class is defined within another Form, or Class, does not mean it has a 'Parent.

The compiler tries to create the 'DoublyLinkedList Class, and finds no usable reference to an instance of the 'txtOutput TextBox, so it produces the error you see.

Even though your defining an EventHandler within your nested Class doesn't cause a compile error: you can't set an EventHandler at design-time for the Click Event of 'btnExecute because it is not visible in the Property Browser. You'd have to set the EventHandler in code, which you don't do.

The "fix" is easy: move the code for the Click EventHandler of 'btnExecute so it's inside Form1, not inside the Class 'DoublyLinkedLists.

And after you do that, you'll need to make some other adjustments:
private void btnExecute_Click(object sender, EventArgs e)
{
    DoublyLinkedList dll = new DoublyLinkedList();
    //add new nodes
    dll.AddNode("Tom");
    dll.AddNode("Jack");
    dll.AddNode("Mary");
    dll.AddNode("Meir");
    dll.AddNode("Toxic");
    dll.Find("Meir");
    //dll.Remove("Toxic");
    //print nodes
    txtOutput.Text = dll.PrintNode();
}

请注意,对dll.Remove的调用已被注释掉:它会导致错误,因为在DoublyLinkedList的代码中,您没有处理previous节点的情况当前节点是'null。



另请注意,您的'Find方法返回'节点,'删除返回'字符串:您不是使用返回的内容不会导致错误,但我建议你学习习惯,总是使用你创建的方法返回;或者,让方法返回'void。



fyi:如果你想疯狂思考使用'String和'字符串之间的区别:[ ^ ]。

Note that the call to dll.Remove is commented out: it will cause an error because in your code for DoublyLinkedList you are not handling the case where the "previous" node of a "current" node is 'null.

Note also that your 'Find method returns a 'Node, and 'Remove returns a 'string: that you don't use what is returned will not cause an error, but I suggest you learn the habit of always using what methods you create return; or, make the methods return 'void.

fyi: if you wish to "go nuts" thinking about the difference between using 'String and 'string: [^].


如上所述,类Form1不会在类DoublyLinkedList中继承。



您需要提供文本框的静态版本



As said, the class Form1 is not inherited in class DoublyLinkedList.

You need to provide an static version of your textbox

public partial class Form1 : Form
    {
        static TextBox txtOutputObj;
        
        public Form1()
        {
            InitializeComponent();
            txtOutputObj = txtOutput;
        }

        class DoublyLinkedList
        {
         private void btnExecute_Click(object sender, EventArgs e)
            {
                DoublyLinkedList dll = new DoublyLinkedList();
                //add new nodes
                dll.AddNode("Tom");
                dll.AddNode("Jack");
                dll.AddNode("Mary");
                dll.AddNode("Meir");
                dll.AddNode("Toxic");
                Find("Meir");
                Remove("Toxic");
                //print nodes
                txtOutputObj.Text = dll.PrintNode();
            }
        }
    }


这篇关于课堂上的错误&访问非静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆