简体   繁体   English

C#For循环和数组

[英]C# For loop and arrays

I need help with this loop and displaying the array correctly into a label. 我需要有关此循环的帮助,并将数组正确显示在标签中。 Here is the Code 这是代码

private void ErrorEquipment()
{
    string[] Equips = { "Projector", "Computer", "Network", "Webcam" };

    for (int i = 0; i <= 3; i++)
    {
        if (venue.Equipment[i] == true)
        {
            lblEquipment.Text = ("Room Has " + Equips[i]);
        }
    }
}

What I want is to check if the venue has the equipment and if so it, display what equipment the venue has in a label. 我要检查的是场地是否有设备,如果有,请在标签上显示场地有什么设备。 Currently it checks that but if a venue has more than one equipment available and it overwrites the first equipment that was put in the label. 目前,它会检查但场所是否有多个可用设备,并且会覆盖标签中放置的第一个设备。

That's because you redefine your variable lblEquipment.Text in every iteration. 那是因为您在每次迭代中都重新定义了变量lblEquipment.Text Try this instead: 尝试以下方法:

    lblEquipment.Text = "Room Has ";
    for (int i = 0; i <= 3; i++)
    {
        if (venue.Equipment[i] == true)
        {
            lblEquipment.Text += Equips[i] + " ";
        }
    }

You can do it something like this. 您可以这样做。

lblEquipment.Text = "Room Has ";
for (int counter = 0; counter <= 3; counter++)
{
    if (venue.Equipment[counter] == true)
        lblEquipment.Text += Equips[counter] + " ";
}

But this is too inefficient because when you concatenate strings, it always creates a new instance of string because string is immutable , and since you're in a for loop, the instance you're creating is being wasted. 但这效率太低了,因为在连接字符串时,由于string不可变的 ,它总是创建一个new instance of string ,并且由于您处于for循环中,因此正在创建的实例被浪费了。

To better implement this, you must create a StringBuilder object and then ToString it after the loop. 为了更好地实现这一点,您必须创建一个StringBuilder对象,然后在循环之后将其ToString Something like this. 这样的事情。

StringBuilder builder = new StringBuilder();
builder.Append("Room Has ");
for (int counter = 0; counter <= 3; counter++)
{
    if (venue.Equipment[counter] == true)
        builder.Append(Equips[counter] + " ");
}
lblEquipment.Text = builder.ToString();

More info about Immutable objects here: C# Tips & Tricks: Immutable Types 有关此处的Immutable对象的更多信息: C#技巧与窍门:不可变的类型

you are replacing each time Label lblEquipment value. 您每次更换lblEquipment标签值。

you need to do String Concatenation instead of just assigning new value. 您需要执行字符串连接,而不仅仅是分配新值。

Use a variable to Store result and then display: 使用变量存储结果,然后显示:

 string rooms ="Room Has ";
 for (int i = 0; i <= 3; i++) 
{ 
  if (venue.Equipment[i] == true) 
     rooms += ","+Equips[i]; // concatenating with previous value
}
lblEquipment.Text = rooms;

A linq inspired solution would be: 受linq启发的解决方案是:

var result = venue.Equipment.Select((v, index) => new { v, index })
                            .Where(pair => pair.v == true && pair.index <=3)
                            .Select(pair => Equips[pair.index]);

lblEquipment.Text = "Room Has "+ string.Join(",",result);

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

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