简体   繁体   English

C#WinForm枚举ToString

[英]C# WinForm Enum ToString

// in general i take random colors from an Enum and insert that into a new array. //通常,我从枚举中获取随机颜色,然后将其插入到新数组中。 when i ask by BoxMessage inside the loop For each Array[i] i get random colors as expected. 当我在循环内通过BoxMessage询问时,对于每个Array [i],我都会得到预期的随机颜色。 If i comment (\\) the message box and ask for Array[1-5] outside the loop, i get the same color for all 5 message box. 如果我注释(\\)消息框并在循环外请求Array [1-5],则所有5个消息框的颜色都相同。 I believe it related to the fact that my message box inside the loop successfully convert the Enum into a string, when i tried to convert the whole array in the loop ( array[i].ToString(); ) it didn't quite work. 我认为这与以下事实有关:当我尝试在循环中转换整个数组(array [i] .ToString();)时,循环内的消息框已成功将Enum转换为字符串。 。 please advise.... 请指教....

    public void GetArray()
    {


        array = new EnumColor[5];
        for (int i = 0; i < 5; i++)
        {
            rnd = new Random();
            int rndnum = rnd.Next(0, 4);
            array[i] = (EnumColor)rndnum;

            MessageBox.Show(array[i].ToString());


        }
        MessageBox.Show(array[0].ToString());
        MessageBox.Show(array[1].ToString());
        MessageBox.Show(array[2].ToString());
        MessageBox.Show(array[3].ToString());
        MessageBox.Show(array[4].ToString());


    }

You are getting the same color because you are declaring random instance each time in your loop, just move the declaration outside: 之所以得到相同的颜色,是因为每次在循环中都声明random实例时,只需将声明移到外部即可:

rnd = new Random();
for (int i = 0; i < 5; i++)
{ ... }

If you like to learn more about why Random class works that way you can take a look at this answer . 如果您想了解更多有关Random类为何以这种方式工作的信息,则可以查看此答案

 for (int i = 0; i < 5; i++)
    {
        rnd = new Random();
        int rndnum = rnd.Next(0, 4);
        array[i] = (EnumColor)rndnum;

        MessageBox.Show(array[i].ToString());


    }

You're overwriting elements in the array, don't do this. 您正在覆盖数组中的元素,请不要这样做。

Retrieve elements into a temporary local variable instead (and use .Length instead of a hardcoded length) 而是将元素检索到临时局部变量中(并使用.Length而不是硬编码的长度)

The bug itself is caused by Random being incorrectly seeded, it's being regerated on every iteration. 该错误本身是由Random的错误种子引起的,它在每次迭代时都会重新发芽。

Random rnd = new Random();

for (int i = 0; i < array.Length; i++)
{
    int rndnum = rnd.Next(0, 4);
    EnumColor selected = (EnumColor)array[i];

    MessageBox.Show( selected .ToString() );
}

The problem is indeed the Random being re-created for every loop. 问题的确是为每个循环重新创建随机数。 But here's the full explanation. 但这是完整的解释。

When you create a Random, it uses the Environment.TickCount as seed value. 创建随机数时,它将使用Environment.TickCount作为种子值。 (milliseconds since system startup). (自系统启动以来的毫秒数)。 So if your loop is fast enough, this will always be the same value, so the rnd.Next function will always return the same value. 因此,如果循环足够快,则该值将始终为相同值,因此rnd.Next函数将始终返回相同的值。

So now for the tricky part.. why did it work with the messagebox inside the loop? 因此,对于棘手的部分,..为什么它与循环内的消息框一起使用? By showing the message, the loop became slower by waiting for you clicking the ok button. 通过显示该消息,等待您单击“确定”按钮,循环变慢了。 Therefore the next loop, the seed was indeed changed. 因此,下一个循环确实更改了种子。 That's why you got another value. 这就是为什么您获得另一个价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM