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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JimBrandleyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
primeMover2004Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.