填写哈希表在c#中通过它进行处理并在aTextBox中显示

时间:2015-08-08 18:52:23

标签: c# arrays textbox hashtable

我正在尝试填充哈希表,密钥只是一个数字ID,内容是一个整数数组。我无法在TextBox中显示它,我认为问题来自填充和显示。

填写代码是:

        Hashtable map = new Hashtable();

        for (int cooX = 0, cont = 0 ; cooX < maxX; cooX++)
        {
            for (int cooY = 0; cooY < maxY; cooY++)
            {
                for (int cooZ = 0; cooZ < maxZ; cooZ++)
                {
                    coordinate[0] = 0 + cooX;
                    coordinate[1] = 0 + cooY;
                    coordinate[2] = 0 + cooZ;

                    map.Add(cont, coordinate);

                    cont++;
                }
            }
        }

显示部分是:

        foreach ( DictionaryEntry i in map )
        {
            TextBox txt = new TextBox();

            txt.AppendText(i.Key + " : " + i.Value);

            MainGrid.Children.Add(txt);
        }

TextBox显示:

“0:System.Int32 []”

谢谢大家。

编辑:哇,这对我来说是不可思议的愚蠢,我已经改变了我的代码,所以它现在只是通过在forfor之前声明TextBox并在它之后显示它来有效地显示Hashtable。

新代码是:

        TextBox txt = new TextBox();

        foreach ( DictionaryEntry i in map )
        {
            txt.AppendText(i.Key + " : " + i.Value + "\n");
        }

        MainGrid.Children.Add(txt);

所以它一定只是一个转换问题。现在去看看,对不起这个大错误。

新输出是:

(...)     3:System.Int32 []     2:System.Int32 []     1:System.Int32 []     0:System.Int32 []

循环的新代码,由以下msmolcic提供,是:

foreach ( DictionaryEntry i in map )
{
int[] coords = (int[])i.Value;

txt.AppendText(string.Format("{0} : [ x={1}, y={2}, z={3} ]\n", i.Key, coords[0], coords[1], coords[2]));
}

现在它显示如下,但仍然存在:

4 : [ x=4, y=4, z=4 ]
3 : [ x=4, y=4, z=4 ]
2 : [ x=4, y=4, z=4 ]
1 : [ x=4, y=4, z=4 ]
0 : [ x=4, y=4, z=4 ]

2 个答案:

答案 0 :(得分:1)

您对地图中的所有元素使用相同的坐标实例。因此,在每次迭代中,您都会覆盖其内容 您必须每次都创建一个新实例,它将解决您的问题

修改

如果你想要一个更干净的代码,我建议你使用更面向对象的方法并定义你自己的Coordinate类。然后,您可以覆盖ToString方法。如果可能的话,你也应该用字典替换你的Hashtable,因为这个字典是

var dict = new Dictionary<int, Coordinate>();
var count = 0;

for (var cooX = 0; cooX < 2; cooX++)
{
    for (var cooY = 0; cooY < 2; cooY++)
    {
        for (var cooZ = 0; cooZ < 2; cooZ++)
        {

            dict.Add(count++, new Coordinate { X = cooX, Y = cooY, Z = cooZ });
        }
    }
}

TextBox txt = new TextBox();
foreach (var i in dict)
{
    var coord = i.Value;
    txt.AppendText(string.Format("{0} : {1}\n", i.Key, coord));
}

MainGrid.Children.Add(txt);

坐标:

class Coordinate
{
    public int X { get; set; }
    public int Y { get; set; }
    public int Z { get; set; }

    public override string ToString()
    {
        return string.Format("[ x={0}, y={1}, z={2} ]", X, Y, Z);
    }
}

如果你已经在VS2015上,你也可以使用字符串插值来用更可读的语法替换string.Format:

    public override string ToString()
    {
        return $"[ x={X}, y={Y}, z={Z} ]";
    }

答案 1 :(得分:0)

sroll和msmolcic在同一个线程中提供了正确的解决方案,每个问题都解决了。所以,为了保持清洁,我将在这里发布最终代码。如果有人为此提供更好,更清洁的代码,我会选择您的答案作为更好的解决方案:

        Hashtable map = new Hashtable();

        TextBox txt = new TextBox();

        int[] coordinate = new int[3];

        for (int cooX = 0, cont = 0; cooX < 2; cooX++)
        {
            for (int cooY = 0; cooY < 2; cooY++)
            {
                for (int cooZ = 0; cooZ < 2; cooZ++)
                {
                    coordinate[0] = 0 + cooX;
                    coordinate[1] = 0 + cooY;
                    coordinate[2] = 0 + cooZ;

                    int[] coo = new int[3] { coordinate[0], coordinate[1], coordinate[2] };

                    map.Add(cont, coo);

                    cont++;
                }
            }
        }

        foreach (DictionaryEntry i in map)
        {
            int[] coords = (int[])i.Value;

            txt.AppendText(string.Format("{0} : [ x={1}, y={2}, z={3} ]\n", i.Key, coords[0], coords[1], coords[2]));
        }

        MainGrid.Children.Add(txt);