-1
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;
}

我已经尝试了上面的编码,但它不起作用,我只是把它放在那里作为我想要实现的示例

4

3 回答 3

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使用这种方法(您可以在函数内初始化,也可以作为字段初始化)。

于 2011-12-21T13:10:50.643 回答
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;
}

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

于 2011-12-21T13:22:02.450 回答
-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");
        }
于 2016-02-01T14:39:43.193 回答