For Loop:我用变量名

时间:2011-12-21 13:07:27

标签: c# variables for-loop names

SubnetConvert SubnetOctet1 = new SubnetConvert();
SubnetConvert SubnetOctet2 = new SubnetConvert();
SubnetConvert SubnetOctet3 = new SubnetConvert();
SubnetConvert SubnetOctet4 = new SubnetConvert();

    int Octet1 = int.Parse(txtOctet1.Text);
    SubnetOctet1.OctetConvert = Octet1;
    lblOctet1.Text = SubnetOctet1.SendBinary;

    int Octet2 = int.Parse(txtOctet2.Text);
    SubnetOctet2.OctetConvert = Octet2;
    lblOctet2.Text = SubnetOctet1.SendBinary;

    int Octet3 = int.Parse(txtOctet3.Text);
    SubnetOctet3.OctetConvert = Octet3;
    lblOctet3.Text = SubnetOctet1.SendBinary;

    int Octet4 = int.Parse(txtOctet4.Text);
    SubnetOctet4.OctetConvert = Octet4;
    lblOctet4.Text = SubnetOctet1.SendBinary;

是否可以将所有这些代码放在For循环中,如

For (int i = 1; i <=4; i++)
{
SubnetConvert SubnetOctet[i] = new SubnetConvert();

    int Octet[i] = int.Parse(txtOctet[i].Text);
    SubnetOctet[i].OctetConvert = Octet[i];
    lblOctet[i].Text = SubnetOctet[i].SendBinary;
}

我已经尝试过上面的编码并且它不起作用,我只是把它放在那里我想要实现的目标

3 个答案:

答案 0 :(得分:3)

代码示例不是可能的 - 如您所示,不支持控件数组。

更好的方法是编写一个封装重复代码并传入不同参数的函数。

private void SetBinaryValue(string value, Label display)
{
    int Octet = int.Parse(value);
    SubnetOctet.OctetConvert = Octet;
    display.Text = SubnetOctet.SendBinary;
}

您可以这样调用此函数:

SetBinaryValue(txtOctet1.Text, lblOctet1);
SetBinaryValue(txtOctet2.Text, lblOctet2);

请注意,此方法只需要一个SubnetConvert(您可以在函数内初始化,也可以作为字段)。

答案 1 :(得分:0)

完全可以使用FindControl循环命名控件:

var subnetOctet = new SubnetConvert();

for (int i = 1; i <= 4; ++i) {
    // ID suffix as string
    var indexText = i.ToString(CultureInfo.InvariantCulture);

    // ID of TextBox and Label
    var textBoxId = "txtOctet" + indexText;
    var labelId = "lblOctet" + indexText;

    // The TextBox and the Label
    var textBox = (TextBox)FindControl(textBoxId);
    var label = (Label)FindControl(labelId);

    // Parse the value into an int
    int octet = int.Parse(textBox.Text);

    subnetOctet.OctetConvert = octet;

    // Update the TextBox's Test
    label.Text = subnetOctet.SendBinary;
}

使用此方法的一个优点是您可以动态添加更多控件,甚至可以以编程方式添加更多控件,如果您跟踪需要处理的子网数量,则无需更新代码。

答案 2 :(得分:-1)

你也可以用你的对象作为元素创建一个数组,然后循环遍历数组并根据循环位置的数组位置执行函数;

Dog pet1 = new Dog();
        Dog pet2 = new Dog();
        Dog pet3 = new Dog();
        Dog pet4 = new Dog();

        //create a list of pets and add your pets to them
        List<Dog> pets = new List<Dog>();
        pets.Add(pet1);
        pets.Add(pet2); 
        pets.Add(pet3);
        pets.Add(pet4);


        //Using a for each loop to go through each element in the array and execute identical actions on each 
        //element
        foreach (Dog pet in pets)
        {
            pet.SetName("Fido");
        }

        //or create a for each loop that will allow you to know the position 
        //you are currenly at in the arry as the integer of i increments in the loop
        for (int i = 0; i <= pets.Count; i++)
        {
            pets[i].SetName("Fido");
        }

理想情况下,您要做的是创建单个对象,并通过另一个循环将对象的多个实例插入到列表中,然后使用foreach或for循环访问列表的元素以操作单个实例。

Dog dog = new Dog();

        //create a list of pets and add your pets to them
        List<Dog> pets = new List<Dog>();

        for (int i = 0; i <= 5; i++)
        {
            pets.Add(dog);
        }

        //Using a for each loop to go through each element in the array and execute identical actions on each 
        //element
        foreach (Dog pet in pets)
        {
            pet.SetName("Fido");
        }