ColdFusion & .NET: Comparing List Functions

It’s been awhile since I have added a new post to the series that compares code in ColdFusion & .NET. Today’s post covers something I use quite a bit throughout some of my applications: storing values in a list type format. If you have programmed in ColdFusion for years, you take the built in list functions for granted. Today’s example covers storing a simple list of ID’s in a variable. You could use this for storing user group associations or something like that. The example will show you how to add an item to the list and then search the list to see if a certain value is stored within it.

This code is fairly simple. We create a UUID, use the ListAppend function to add the value to our list, then use ListFindNoCase to see if our value is in the list.

Now for the .NET code:

<code lang="csharp[lines]">using System;<br></br>
using System.Collections.Generic;<br></br>
using System.Web;<br></br>
using System.Web.UI;<br></br>
using System.Web.UI.WebControls;<br></br>
using System.Collections;```
public partial class list_test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Guid newID = System.Guid.NewGuid();
ArrayList idList = new ArrayList();
idList.Add(newID);
if (idList.Contains(newID) == true)
foundLabel.Text = "Item Found";
else
foundLabel.Text = "Item Not Found";
}
}
This code is fairly simple as well. The big difference between the two languages is that .NET treats the list as an array, so we first set uo our ArrayList object, add our Guid value to the ArrayList, then search for our value using the Contains function. We then update the value of a label on our front end page depending on our results.
So as you can see using lists is easy no matter which language you use. Both languages also allow you to store your lists in the session scope as well. The one pointer for doing this in .NET is you must cast your value back out to an ArrayList object when reading it from the session: