Warning: INSERT command denied to user 'dbo292345962'@'74.208.59.105' for table 'watchdog' query: INSERT INTO watchdog (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (0, 'php', '%message in %file on line %line.', 'a:4:{s:6:\"%error\";s:12:\"user warning\";s:8:\"%message\";s:400:\"INSERT command denied to user 'dbo292345962'@'74.208.59.105' for table 'captcha_sessions'\nquery: INSERT into captcha_sessions (uid, sid, ip_address, timestamp, form_id, solution, status, attempts) VALUES (0, '67cc4db760dc962908c0b28e523da4c0', '34.204.175.38', 1569103443, 'comment_form', '293cf5880d7c9ddcbc3cf82ebcbf223f', 0, 0)\";s:5:\"%file\";s:62:\"/homepages/25/d199835659/htdocs/ID/modules/captcha/captcha.inc\";s:5:\"%line\";i:99;}& in /homepages/25/d199835659/htdocs/ID/includes/database.mysql.inc on line 135
Linked Lists for Beginners | Inferno Development

Linked Lists for Beginners

This article will demonstrate the very basics of working with linked lists in C#. Linked lists are perhaps the most powerful forms of data storage, and are used in almost every program. If you are new to C# or are more interested behind the List<> basics, then this tutorial is for you. A very basic understanding of the language and its syntax is required.

Creating a new instance of a linked list.

Very simply, creating a new instance of a linked list is like for any other object. The syntax is simple. Here is a example linked list with no assigned data type, and so will not function.

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace InfernoDevelopment
{
    public partial class idLL : Form
    {
        private List<> idList;
        public Form1()
        {
            InitializeComponent();
            idList = new List<>();
        }
    }
}

Here i have left <> blank, between the <> you would assign a data type for what the list is to hold.

Using basic data types.

With the example made in the first section, we now want our list to hold data. Now for this we will use a common data type, a string. A string is a piece of text, it could include integers or special chars. And to demonstrate this, we will be creating a form to submit data and then a button to return the data we have just submitted.

private List<> idList;
public idLL()
{
    InitializeComponent();
    idList = new List<>();
}

Now with that code we now want to make sure our List will contain string, and only string. Note that it doesnt always have to be a string, any data type such as a float, decimal, int, byte, uint, Bitmap, Icon, Button, Label etc.

private List<string> idList;
public idLL()
{
    InitializeComponent();
    idList = new List<string>();
}

Now for this, i created a form, shown in the screen shot below, and every time i press submit text, it will add the text from the text box to the list.

Then when i press dump data, it will past all data within the list to the rich text box. The source files for this are included in the attachment, you will need to register for that.

Notice how i defined the list as a global variable, and therefore will remain in scope when i choose to add extra functions.

Linked Lists Basic Data Type Example

Now for the click event for the Submit button, we want to add the text from the text box into the list. To do that we would do...

private void btnSubmit_Click(object sender, EventArgs e)
{
    idList.Add(txtData.Text); //Add text to list
    txtData.Text = ""; //Clear text from text box
}

Now to dump all the items to the RTB, we will again create a click event for the Dump Data button and add the following code...

private void btnDump_Click(object sender, EventArgs e)
{
    foreach (string itm in idList) { //Loop through each item in list
        txtDump.AppendText(itm + "\n\r"); //Add item to the RTB
    }
    idList.Clear(); //Clear all items from the list
}

I have annotated the code to show you what goes on, but all thats required is a basic foreach loop.

So thats just about it, if you register you can download the source for this program which is called idLLbasic.zip. You will need Visual Studio 2008 to open the solution files.

An example of this program in use, is shown below:

Linked Lists In Action

An extension to this article as to using Linked Lists using custom data types is on the way. In that article i will really demonstrate the power of linked lists.

AttachmentSize
idLLbasic.zip37.43 KB

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.