C# associative array problem

Goal:
How can i create the following in c#



         $resultset = array(
                array("key"=>"unique key name 0", "x"=>0, "y"=>0 ), //row 1
                array("key"=>"unique key name 1", "x"=>1, "y"=>1 ), //row 2
                array("key"=>"unique key name 2", "x"=>2, "y"=>2 ), //row 3
                array("key"=>"unique key name 3", "x"=>3, "y"=>3 ), //row 4
                array("key"=>"unique key name 4", "x"=>4, "y"=>4 )  //row 5
          )

          return $resultset;

         //would i then use IEnumerator to loop through my result set?  MoveNext()?


Associative array is Dictionary/Hashtable and from what I see you want a Dictionary of Dictionaries.

To me it looks more like a list of dictionaries. Each dictionary seems to have the same set of keys, which leads me to think that what leblanc really needs is a DataTable.

A .NET DataTable is a list/set of similar structured instances (rows). From 2.0 it can exist on its own, in 1.x it must be part of a DataSet.

With .NET 2.0 you create a datatable declaratively through a DataSet declaration or you construct it with code. A DataSet declaration is in fact merely a XML schema where repetitive elements are interpreted as tables.

With code you could do this:

        DataTable t = new DataTable();
        t.Columns.Add("key", typeof(string));
        t.Columns.Add("x", typeof(int));
        t.Columns.Add("y", typeof(int));

        t.Rows.Add("unique key name 0", 0, 0);
        t.Rows.Add("unique key name 1", 1, 1);
        t.Rows.Add("unique key name 2", 2, 2);
        t.Rows.Add("unique key name 3", 3, 3);
        t.Rows.Add("unique key name 4", 4, 4);

A datatable would work. Seems a bit overkill to me.
I would still use a dictionary/hashtable.

In .NET 1.* I would use System.Collections.HashTable
In .NET 2.0 I would use System.Collections.Generic.Dictionary

So use string as a key, and since it looks like you are getting an X & Y value, why not use System.Drawing.Point as the value

So for .NET 1.*


[size=2][color=#0000ff]private [/color][/size][size=2][color=#008080]Hashtable[/color][/size][size=2] getDataV1()[/size]
[size=2]{[/size]
[size=2]System.Collections.[/size][size=2][color=#008080]Hashtable[/color][/size][size=2] hash = [/size][size=2][color=#0000ff]new[/color][/size][size=2] System.Collections.[/size][size=2][color=#008080]Hashtable[/color][/size][size=2]();[/size]
[size=2]hash.Add([/size][size=2][color=#800000]"unique key name 0"[/color][/size][size=2], [/size][size=2][color=#0000ff]new [/color][/size][size=2][color=#008080]Point[/color][/size][size=2](0, 0));[/size]
[size=2]hash.Add([/size][size=2][color=#800000]"unique key name 0"[/color][/size][size=2], [/size][size=2][color=#0000ff]new [/color][/size][size=2][color=#008080]Point[/color][/size][size=2](1, 1));[/size]
[size=2]hash.Add([/size][size=2][color=#800000]"unique key name 1"[/color][/size][size=2], [/size][size=2][color=#0000ff]new [/color][/size][size=2][color=#008080]Point[/color][/size][size=2](2, 2));[/size]
[size=2]hash.Add([/size][size=2][color=#800000]"unique key name 2"[/color][/size][size=2], [/size][size=2][color=#0000ff]new [/color][/size][size=2][color=#008080]Point[/color][/size][size=2](3, 3));[/size]
[size=2]hash.Add([/size][size=2][color=#800000]"unique key name 3"[/color][/size][size=2], [/size][size=2][color=#0000ff]new [/color][/size][size=2][color=#008080]Point[/color][/size][size=2](4, 4));[/size]
[size=2]hash.Add([/size][size=2][color=#800000]"unique key name 4"[/color][/size][size=2], [/size][size=2][color=#0000ff]new [/color][/size][size=2][color=#008080]Point[/color][/size][size=2](5, 5));[/size]
 
[size=2][color=#0000ff]return[/color][/size][size=2] hash;[/size]
[size=2]}[/size]

And for .NET 2.0


[size=2][color=#0000ff]private [/color][/size][size=2][color=#008080]Dictionary[size=2][color=#333333]<[/color][/size][size=2][color=#0000ff]string[/color][/size][size=2][color=#333333], [/color][/size][size=2][color=#008080]Point[/color][/size][/color][size=2][color=#333333]>[/color][/size][/size][size=2] getDataV2()[/size]
[size=2]{[/size]
[size=2][size=2]System.Collections.Generic.[/size][size=2][color=#008080]Dictionary[/color][/size][size=2]<[/size][size=2][color=#0000ff]string[/color][/size][size=2], [/size][size=2][color=#008080]Point[/color][/size][size=2]> hash = [/size][size=2][color=#0000ff]new [/color][/size][size=2][color=#008080]Dictionary[/color][/size][size=2]<[/size][size=2][color=#0000ff]string[/color][/size][size=2], [/size][size=2][color=#008080]Point[/color][/size][size=2]>();
[/size][/size][size=2]hash.Add([/size][size=2][color=#800000]"unique key name 0"[/color][/size][size=2], [/size][size=2][color=#0000ff]new [/color][/size][size=2][color=#008080]Point[/color][/size][size=2](0, 0));[/size]
[size=2]hash.Add([/size][size=2][color=#800000]"unique key name 0"[/color][/size][size=2], [/size][size=2][color=#0000ff]new [/color][/size][size=2][color=#008080]Point[/color][/size][size=2](1, 1));[/size]
[size=2]hash.Add([/size][size=2][color=#800000]"unique key name 1"[/color][/size][size=2], [/size][size=2][color=#0000ff]new [/color][/size][size=2][color=#008080]Point[/color][/size][size=2](2, 2));[/size]
[size=2]hash.Add([/size][size=2][color=#800000]"unique key name 2"[/color][/size][size=2], [/size][size=2][color=#0000ff]new [/color][/size][size=2][color=#008080]Point[/color][/size][size=2](3, 3));[/size]
[size=2]hash.Add([/size][size=2][color=#800000]"unique key name 3"[/color][/size][size=2], [/size][size=2][color=#0000ff]new [/color][/size][size=2][color=#008080]Point[/color][/size][size=2](4, 4));[/size]
[size=2]hash.Add([/size][size=2][color=#800000]"unique key name 4"[/color][/size][size=2], [/size][size=2][color=#0000ff]new [/color][/size][size=2][color=#008080]Point[/color][/size][size=2](5, 5));[/size]
 
[size=2][color=#0000ff]return[/color][/size][size=2] hash;[/size]
[size=2]}[/size]

And finally

[size=2]
[/size][size=2][color=#0000ff]private [/color][/size][size=2][color=#008080]IDictionary[/color][/size][size=2] getDBChartData()[/size]
[size=2]{[/size]
[color=#008000]// And you could do something like [/color]
[size=2][color=#0000ff]#if[/color][/size][size=2][color=#000000] V2[/color][/size]
[color=#000000][size=2]return[/size][size=2] getDataV2();[/size][/color]
[size=2][color=#0000ff]#else[/color][/size]
[color=#0000ff][size=2]return[/size][/color][size=2] getDataV1();[/size]
[size=2][color=#0000ff]#endif
[/color][/size]}[size=2]
[/size]

And to work with the data


[size=2][color=#008080]IDictionary[/color][/size][size=2][color=#000000] d = getDBChartData();[/color][/size]
[size=2][color=#0000ff]foreach[/color][/size][size=2] ([/size][size=2][color=#0000ff]object[/color][/size][size=2] o [/size][size=2][color=#0000ff]in[/color][/size][size=2] d.Keys)[/size]
[size=2]{[/size]
[size=2][color=#008080]Point[/color][/size][size=2] p = ([/size][size=2][color=#008080]Point[/color][/size][size=2])d[o];[/size]
[size=2][color=#008000]// now you can juse use p.X and p.Y[/color][/size]
[size=2]}
[/size]

I don’t see the point in having table[key][“X”] to get X when you could have dictionary[key].X

Now if you wanted something more than just a point, make your own custom structure to use with the hashtable/dictionary.

Benjymouse - thanks for the help. A table does represent a db result set the best.

Sei - thanks also. I should have been a little more descriptive in the unique key column.

The goal is to plot multiple lines on the plot area. Instead of making several calls to the db for each line graph, I retrieve all the lines in one db call but have to parse the result set on the script side.



    private DataTable getDBChartData() {

        DataTable t = new DataTable();
        t.Columns.Add("GraphName", typeof(String));
        t.Columns.Add("x", typeof(Int32));
        t.Columns.Add("y", typeof(Double));

        t.Rows.Add("graph1", 1, 1.666666667);
        t.Rows.Add("graph1", 2, 0.833333333);
        t.Rows.Add("graph1", 3, 0.833333333);
        t.Rows.Add("graph1", 4, 0.833333333);
        t.Rows.Add("graph1", 5, 0.75);
        t.Rows.Add("graph1", 6, 0.722222222);
        t.Rows.Add("graph1", 7, 0.702380952);
        t.Rows.Add("graph1", 8, 0.6875);
        t.Rows.Add("graph1", 9, 0.675925926);
        t.Rows.Add("graph1", 10, 0.666666667);


        t.Rows.Add("graph2", 1, 1.566666667);
        t.Rows.Add("graph2", 2, 0.933333333);
        t.Rows.Add("graph2", 3, 0.933333333);
        t.Rows.Add("graph2", 4, 0.933333333);
        t.Rows.Add("graph2", 5, 0.85);
        t.Rows.Add("graph2", 6, 0.74522222);
        t.Rows.Add("graph2", 7, 0.732380952);
        t.Rows.Add("graph2", 8, 0.8875);
        t.Rows.Add("graph2", 9, 0.275925926);
        t.Rows.Add("graph2", 10, 0.616666667);

        return t;
    }


THE PROBLEM
I need to take the db result set and convert it to Point objects so i can call DrawLines. Can someone help me with my function parseDbResultSet(ref DataTable dataTable)

I’m sure this can be solved with a nested hash or nested dictionary. Note: all the points need to be grouped with the name of the graph so I know the symbolic name needed in the graph’s key.

The goal
The goal would be to create the following by looping through the db result set, DataTable (foreach (DataRow dataRow in dataTable.Rows)), and append the hash/dictionary list:

Here is my php version.


Final hash view:
$DrawLineData = array(
					
						"Line1"=>array(
									new Point(0,0),
									new Point(0,0),
									new Point(0,0),
									new Point(0,0),
									new Point(0,0),
									new Point(0,0),
									new Point(0,0)
								)
					
						"Line2"=>array(
									new Point(0,0),
									new Point(0,0),
									new Point(0,0),
									new Point(0,0),
									new Point(0,0),
									new Point(0,0),
									new Point(0,0)
								)
					)





Example of how it was created:

//dynamically adding more points to the hash line1
$DrawLineData["Line1"][] = new Point(0,0);
$DrawLineData["Line1"][] = new Point(0,0);

//dynamically adding more points to the hash line2
$DrawLineData["Line2"][] = new Point(0,0);
$DrawLineData["Line2"][] = new Point(0,0);


in the future:
to plot line 1: chartGraphic.DrawLines(pen, DrawLineData["Line1"]);
to plot line 2: chartGraphic.DrawLines(pen, DrawLineData["Line2"]);


The project i’m working on


filename: Handler.ashx

<%@ WebHandler Language="C#" Class="Handler" %>
//Generic Handler
using System;
using System.Web;
using System.Data;                // DataTable containing a list of dictonaries

using System.Collections;         //
using System.Collections.Generic; // Dictionary

using System.Drawing;			  // Graphics, Pen, SolidBrush, TextureBrush, Image, Rectangle
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;	  // Dashcap, DashStyle

/* Given a canvas is 904 x 517, the following are visual enticing porportions.
 * Key Area: 180px x 254px
 * Plot Area: 586px x 359px
 * Padding Between Plot and Key: 20px
 * Title Padding: 95px
 */
public class Handler : IHttpHandler {

    // Limits for horizontal and vertical
    private Int32 xMax;
    private Int32 xMin;

    private Int32 yMax;
    private Int32 yMin;

    // X and Y interval
    private Int32 xInterval;
    private Int32 yInterval;

    private Int32 CanvasHeight = 517;
    private Int32 CanvasWidth = 904;



    //Key = line graph unique name,
    //Value = Hex String of colors currently assigned to unique graph line
    private Dictionary<string,string> UsedColors;
    private DataTable GraphLines;


    public void ProcessRequest (HttpContext context) {

        DataTable dataTable = getDBChartData();
        parseDbResultSet(ref dataTable);

        context.Response.ContentType = "image/jpeg";
        //context.Response.Write("Hello World");

        //Width, Height
        Bitmap graphBmp = new Bitmap(CanvasWidth, CanvasHeight);
        Graphics chartGraphic = Graphics.FromImage(graphBmp);
        chartGraphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        chartGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        chartGraphic.FillRectangle(new SolidBrush(Color.White), 0, 0, CanvasWidth, CanvasHeight);



        plotTitle(ref chartGraphic, "Title of Line Graph");
        plotYLabel(ref chartGraphic, "Y Label Title");
        plotXLabel(ref chartGraphic, "X Label Title");

        plotGraphArea(ref chartGraphic);
        plotKeyArea(ref chartGraphic);

        /*
         DataTable GraphLines = new DataTable();
         t.Columns.Add("GraphName", typeof(String));
         t.Columns.Add("Point", typeof(Point));



         Pen pen = new Pen(Color.BlueViolet, 10);

         //  chartGraphic.DrawRectangle(
         //        pen,
         //        10, 10,	// coordinates of upper left hand corner of rectangle
         //        75,		// width of rectangle
         //        50		// height of rectangle
         //    );


         pen.Color = Color.FromArgb(192,192,192);
         pen.Width = 2;
         Point[] ptLines = {
                     new Point(400, 350),
                     new Point(360, 250),
                     new Point(320, 250),
                     new Point(280, 250),
                     new Point(240, 125),
                     new Point(200, 120),
                     new Point(160, 119),
                     new Point(120, 117),
                     new Point(80, 115),
                     new Point(40, 100)
         };


         // ptLines.Length
         chartGraphic.DrawLines(pen, ptLines);
         */

        graphBmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        chartGraphic.Dispose();

    }




    private void plotGraphArea(ref Graphics chart)
    {
        LinearGradientBrush lgBrush = new LinearGradientBrush(
            new Rectangle(
                new Point(0,0),
                new Size(CanvasWidth, CanvasHeight)
            ),
             Color.FromArgb(0x78f4f4f4), //top color
             Color.FromArgb(0x78a8a8a8), //bottom color
            LinearGradientMode.Vertical
        );
        chart.FillRectangle(lgBrush, 0.09070f * CanvasWidth, 0.08897f * CanvasHeight, 0.65044f * CanvasWidth, 0.75999f * CanvasHeight);



    }
    private void plotKeyArea(ref Graphics chart)
    {
        chart.FillRectangle(new SolidBrush(Color.Red), 0.76105f * CanvasWidth, 0.08897f * CanvasHeight, 0.23f * CanvasWidth, 0.75999f * CanvasHeight);
    }
    private void plotTitle(ref Graphics chart, String title)
    {
        Font titlefnt = new Font("Arial", 20, FontStyle.Bold);

        StringFormat drawFormat = new StringFormat();
        drawFormat.LineAlignment = StringAlignment.Center;
        drawFormat.Alignment = StringAlignment.Center; //StringAlignment.Near;

        chart.DrawString(
            title,
            titlefnt,
            new SolidBrush(Color.Black),
            0.40707f * CanvasWidth,  //location is porportional to CanvasWidth
            0.03821f * CanvasHeight, //location is porportional to CanvasHeight
            drawFormat
        );
    }
    private void plotXLabel(ref Graphics chart, String title)
    {
        Font labelfnt = new Font("Times New Roman", 15, FontStyle.Bold);

        StringFormat drawFormat = new StringFormat();
        drawFormat.LineAlignment = StringAlignment.Center;
        drawFormat.Alignment = StringAlignment.Center; //StringAlignment.Near;

        chart.DrawString(
            title,
            labelfnt,
            new SolidBrush(Color.Black),
            0.40707f * CanvasWidth,
            0.98146f * CanvasHeight, //location
            drawFormat
        );
    }
    private void plotYLabel(ref Graphics chart, String title)
    {
        chart.TranslateTransform(0.01406f * CanvasWidth, CanvasHeight / 2);
        chart.RotateTransform(270);

        StringFormat drawFormat = new StringFormat();
        drawFormat.LineAlignment = StringAlignment.Center;
        drawFormat.Alignment = StringAlignment.Center; //StringAlignment.Near;

        //grMyGraphicsObject.RotateTransform(45);
        Font labelfnt = new Font("Times New Roman", 15, FontStyle.Bold);
        chart.DrawString(
            title,
            labelfnt,
            new SolidBrush(Color.Black),
            0, 0, //location
            drawFormat
        );

        //reset translation point
        chart.TranslateTransform(CanvasHeight / 2, -0.01406f * CanvasWidth);
        chart.RotateTransform(-270);
    }








    private void getNextColor() {

    }



    private void parseDbResultSet(ref DataTable dataTable) {
        String previousGraphID = "null";

        foreach (DataRow dataRow in dataTable.Rows)
        {
            if (previousGraphID == dataRow["GraphName"].ToString())
            {

            }
            else
            {
                //Start a new line graph set
                previousGraphID = dataRow["GraphName"].ToString();
            }


           // context.Response.Write("Graph Name: " + dataRow["GraphName"] + " x:" + dataRow["x"] + " y:" + dataRow["y"]);
        }
    }




    //Use DataTable when you have a list of Dictionaries
    private DataTable getDBChartData() {
        DataTable t = new DataTable();
        t.Columns.Add("GraphName", typeof(String));
        t.Columns.Add("x", typeof(Int32));
        t.Columns.Add("y", typeof(Double));

        t.Rows.Add("graph1", 1, 1.666666667);
        t.Rows.Add("graph1", 2, 0.833333333);
        t.Rows.Add("graph1", 3, 0.833333333);
        t.Rows.Add("graph1", 4, 0.833333333);
        t.Rows.Add("graph1", 5, 0.75);
        t.Rows.Add("graph1", 6, 0.722222222);
        t.Rows.Add("graph1", 7, 0.702380952);
        t.Rows.Add("graph1", 8, 0.6875);
        t.Rows.Add("graph1", 9, 0.675925926);
        t.Rows.Add("graph1", 10, 0.666666667);


        t.Rows.Add("graph2", 1, 1.566666667);
        t.Rows.Add("graph2", 2, 0.933333333);
        t.Rows.Add("graph2", 3, 0.933333333);
        t.Rows.Add("graph2", 4, 0.933333333);
        t.Rows.Add("graph2", 5, 0.85);
        t.Rows.Add("graph2", 6, 0.74522222);
        t.Rows.Add("graph2", 7, 0.732380952);
        t.Rows.Add("graph2", 8, 0.8875);
        t.Rows.Add("graph2", 9, 0.275925926);
        t.Rows.Add("graph2", 10, 0.616666667);

        return t;
    }



    public bool IsReusable {
        get {
            return false;
        }
    }

}

Thanks for everyone’s help!