Click here to Skip to main content
16,020,459 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 11-Feb-14 19:40pm
v2
Comments
midnight_ 12-Feb-14 1:26am    
Maybe you provide some more code - but the msg tells you, that the outer type is not inherited.
This means you cannot access it this way. Try to make it static.
Sergey Alexandrovich Kryukov 12-Feb-14 1:39am    
Apparently, static is not what OP wants... :-) Just a bad advice. OP needs understanding, not solution, really.
—SA
BillWoodruff 12-Feb-14 1:33am    
To help you analyze this, we'd need to know inside what Form, or Class, 'DoublyLinkedList is defined in, where 'Find, and 'Remove are defined, where btnExecute_Click is defined, where 'txtOutput is defined, and so on.
Sergey Alexandrovich Kryukov 12-Feb-14 1:39am    
Isn't that apparent that you should provide definitions of both types involved? But don't show all your code. Instead, create a comprehensive simplest possible code sample to reproduce the problem. Please see: SSCCE.
—SA

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:
C#
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();
}
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: [^].
 
Share this answer
 
v3
Comments
Member 10540766 12-Feb-14 2:51am    
Thanks Bill!, but where would i move the find and remove codes to get it to work?
BillWoodruff 12-Feb-14 3:02am    
You don't need to move them; just put the instance name of the 'DoublyLinkedList Class followed by the ".", in this case, 'dll.', in front of the method names. What you need to do to fix the error of logic I observe in the 'Remove method, I am not sure.
Member 10540766 13-Feb-14 18:33pm    
In the remove i want it to remove a node and return a string saying if it worked or not, is it bad to code it that way?
BillWoodruff 13-Feb-14 20:11pm    
No, nothing wrong in wanting to do something with the result of Removing an item from a Collection !

In fact, the 'Remove method returns a boolean value; so this:

bool IsItemRemoved = dll.Remove("Toxic");

will give you a boolean value that you can use to do whatever you want with it.

However, if there is an error of logic in your doubly linked list class, then I am not sure that using 'Remove in this way will prevent an error; you'll have to test your code and see what happens.

While you could wrap the call to 'Remove in a try/catch to recover from any error, that's just "sweeping the problem under the rug."

And, in any case, I am sure you want to fix whatever error in logic is in your double linked list.

good luck, Bill
Member 10540766 13-Feb-14 20:24pm    
I have tested the remove method and it removes perfectly, but does not output the strings such as:

else
{
current.next.prev = current.prev;
current.prev.next = current.next;
current.prev = null;
current.next = null;
Output += "\r\n" + "Node removed from Doubly Linked List" + "\r\n";
}

Also i noticed the find method does not output anything either
As said, the class Form1 is not inherited in class DoublyLinkedList.

You need to provide an static version of your textbox

C#
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();
            }
        }
    }
 
Share this answer
 
Comments
BillWoodruff 12-Feb-14 2:29am    
+4 That is a solution to the specific error the OP reported, but there are other serious problems with the OP's code here.
midnight_ 12-Feb-14 3:22am    
you're right, and your solution explains it better - i just gave it a quick shot :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900