[Class] Associate array in C# .NET

Started by Xhanch Studio, August 05, 2012, 02:56:48 AM

previous topic - next topic
Go Down

Xhanch Studio

August 05, 2012, 02:56:48 AM Last Edit: August 05, 2012, 03:00:00 AM by Xhanch Studio
Associative array is very useful due to its flexibility and expandability. It can contain any data types and unlimited sub arrays. So far, .NET framework has not provide similar functionality with PHP associative array. Don't worry, we can still do it in C#.NET by utilizing dictionary. We have to use dictionary to keep the order of array element entry. If we use hashtable, the element order will be messed up. In this article, we will show you how dictionary can support the flexibility and expandability of PHP associative array.


Source Code

Code Select
//By    : Xhanch Studio
//URL   : http://xhanch.com/

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

namespace AssociativeArray{
    public class AssociativeArray{
        public Dictionary<object, object> Item;

        public AssociativeArray(){
           this.Item = new Dictionary<object, object>();
        }

        public void Add(object name, object value){
        this.Item.Add(name, value);
        }

        public void Add(object name, AssociativeArray value){
        this.Item.Add(name, value.Item);
        }

        public Dictionary<object, object>.KeyCollection GetKeys(){
        return this.Item.Keys;
        }

        public bool InArray(object hook){
        return this.Item.ContainsKey(hook);
        }

        public int Count(){
        return this.Item.Count;
        }

        public static bool IsAssociativeArray(){
        return (this.Item.GetType().ToString().StartsWith("System.Collections.Generic.Dictionary"));
        }
    }
}



Usage Examples

Code Select
using System;
using AssociativeArray;

namespace Test{
    public class Test{
        static void Main(string[] args){
            AssociativeArray arr= new AssociativeArray();
            arr.Add("elm 1", "value 1");
            arr.Add("elm 2", true);
            arr.Add("elm 3", 1);

            //Add a sub array
            AssociativeArray arrChd = new AssociativeArray();
            arrChd .Add("chd 1", "value 1");
            arrChd .Add("chd 2", true);
            arrChd .Add("chd 3", 1);
            arr.Add("elm 4", arrChd);

            // List all AssociativeArray manually
            foreach (string key in arr.GetKeys()){
                //Check if the current element is an associative array
                if(AssociativeArray.IsAssociativeArray(arr.Item[key])){
                    //Accessing sub array
                    AssociativeArray tmp = new AssociativeArray();
                    tmp.Item = arr.Item[key];
                    foreach (string key1 in tmp.GetKeys()){
                        Console.WriteLine(key1 + ": " + tmp.Item[key1 ]);
                    }
                }else
                    Console.WriteLine(key + ": " + arr.Item[key]);
            }

    // Single Item
            Console.WriteLine("User: " + arr.Item["Line 1"]);
        }
    }
}
Best Regards,
Susanto B.Sc
----------------------------------------------------------------------------
Web development services, WordPress plugin and theme development, PSD to XHTML conversion - http://xhanch.com
Read free manga online - http://authrone.com

Go Up