How can I create an associative multi-dimensional array?

I have an existing Javascript array that I made like this:

    var content = new Array();
    content[0] = { id: 'header', width: '500px', height: '100px' };
    content[1] = { id: 'content', width: '100%', height: '600px' };
    content[2] = { id: 'footer', width: '500px', height: '90px' };

So now i can do something like this:
for (int i=0; i < content.Length(); i++)
  Response.Write(content[i]["id"] + "<br/>" + content[i]["width"] + "<br/>" + content[i]["height"]);

How can I accomplish this in C#?
bemara57Asked:
Who is Participating?
 
JimBrandleyConnect With a Mentor Commented:
If you want the ID and the integer values in there, I would create a simple class. As:
public class ElementSize
{
   private string mID = string.Empty;
   private string mWidth = string.Empty;; // Or some default value
   private string mHeight = string.Empty;; // Or some default value

   public string ID
   {
      get { return mID; }
      set { mID = value;
   }
   public string Height
   {
      get { return mHeight; }
      set { mHeight = value;
   }
   public string Width
   {
      get { return mWidth; }
      set { mWidth = value;
   }

   public ElementSize( string id, string width, string height )
   {
      mID = id;
      mWidth = width;
      mHeight = height;
   }
}

Then, you can:
ElementSize[] content = new ElementSize[3];
content[0] = new ElementSize("header", "500px", "100px");
content[1] = new ElementSize("content", "100%", "600px");
content[2] = new ElementSize("footer", "500px", "90px");

for (int i=0; i < content.Length(); i++)
  Response.Write(content[i].ID + "<br/>" + content[i].Width + "<br/>" + content[i].Height);

Jim
0
 
primeMover2004Connect With a Mentor Commented:
This should work
using System;
using System.Collections.Generic;
using System.Text;
 
namespace associative_array
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Dictionary<string, string>> myList = new List<Dictionary<string, string>>();
            AddItem("header", "500px", "100px", myList);
            AddItem("content", "100%", "600px", myList);
            AddItem("footer", "500px", "90px", myList);
            
 
            foreach (Dictionary<string, string> aLine in myList)
            {
                Console.WriteLine(string.Format("id:{0},width:{1};height:{2}", aLine["id"], aLine["width"], aLine["height"]));
            }
            Console.ReadLine();
 
        }
        static void AddItem(string id, string width, string height, List<Dictionary<string, string>> aList)
        {
            Dictionary<string, string> myLine = new Dictionary<string, string>();
            myLine["id"] = id;
            myLine["width"] = width;
            myLine["height"] = height;
            aList.Add(myLine);
        }
    }
}

Open in new window

0
 
bemara57Author Commented:
Thanks primeMover, that worked great. JimBrandley, I never really looked at it like that- I'm going to re-do my logic that way. Thanks.
0
 
JimBrandleyCommented:
My pleasure. Good luck.

Jim
0
 
ColemssCommented:
I see what you are trying to do.. Your example is way off.

The input would be something like this.
                  //Option1
            string[] strarray = new string[] { "header,500px,100px", "content,500px,100px","footer,500px,100px" };

//Option 2
        string[] strArray = new string[3];
        strArray[0] =  "header,500px,100px";
    strArray[1] =  "content,100%,600px";
          strArray[2] = "footer,500px,90px";
          
//Then use something like this to write it out.
          foreach (string s in strArray) {
                        string[] strSplitArray = s.Split(',');
                        string sWs = string.Format("{0}<br/>{1}<br/>{2}", strSplitArray[0], strSplitArray[1], strSplitArray[2]);
                        Console.WriteLine(sWs);
          }

0
All Courses

From novice to tech pro — start learning today.