For 循环:变量名中包含 i

发布于 2024-12-22 13:21:34 字数 1084 浏览 3 评论 0原文

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

我尝试过上面的编码一样,但它不起作用,我只是将其放在那里作为我想要实现的示例

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;

is it possible to put all this code in a For loop like

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

I have tried the coding above and it doesn't work, I have just put it there for an example of what I want to achieve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神经暖 2024-12-29 13:21:34

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

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

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(您可以在函数内初始化它,也可以将其初始化为一个字段)。

The code sample is not something possible - there is no support for control arrays as you have shown.

A better way would be to write a function that encapsulates the repeating code and pass in the differing parameters.

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

You would call this function like so:

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

Note that you only need one SubnetConvert with this approach (which you can either initialize within the function, or as a field).

初见 2024-12-29 13:21:34

使用 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;
}

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

It's perfectly possible to loop through named controls using 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;
}

One advantage to using this method is that you can add more controls on the fly, or even programmatically, and if you keep track of the number of subnets you need to handle, you do not have to update your code.

纸短情长 2024-12-29 13:21:34

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

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");
        }

You could also create an Array with the your objects as the elements and then loop through the array and execute the functions based on the array position at loop position;

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");
        }

Ideally what you will want to do is create a single object and insert multiple instances of the object into the list via another loop and then use the foreach or the for loop to access an element of the list to manipulate a singular instance.

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");
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文