C# For循环:变量名中的i
有可能把所有这些代码放在一个For循环中吗C# For循环:变量名中的i,c#,variables,for-loop,names,C#,Variables,For Loop,Names,有可能把所有这些代码放在一个For循环中吗 SubnetConvert SubnetOctet1 = new SubnetConvert(); SubnetConvert SubnetOctet2 = new SubnetConvert(); SubnetConvert SubnetOctet3 = new SubnetConvert(); SubnetConvert SubnetOctet4 = new SubnetConvert(); int Octet1 = int.Parse(
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(int i=1;i代码示例是不可能的-如您所示,不支持控制数组
更好的方法是编写一个封装重复代码并传入不同参数的函数
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;
}
您可以这样调用此函数:
private void SetBinaryValue(string value, Label display)
{
int Octet = int.Parse(value);
SubnetOctet.OctetConvert = Octet;
display.Text = SubnetOctet.SendBinary;
}
请注意,使用这种方法,您只需要一个子网Convert
(可以在函数中初始化,也可以作为字段进行初始化)。使用FindControl
完全可以循环命名控件:
SetBinaryValue(txtOctet1.Text, lblOctet1);
SetBinaryValue(txtOctet2.Text, lblOctet2);
var subnetocet=new SubnetConvert();
对于(inti=1;i,您还可以创建一个以对象为元素的数组,然后在数组中循环,并根据循环位置处的数组位置执行函数
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;
}
Dog pet1=新狗();
宠物狗2=新狗();
宠物狗3=新狗();
宠物狗4=新狗();
//创建宠物列表并将您的宠物添加到其中
列表宠物=新列表();
pets.Add(pet1);
pets.Add(pet2);
pets.Add(pet3);
pets.Add(pet4);
//使用for-each循环遍历数组中的每个元素,并对每个元素执行相同的操作
//元素
foreach(宠物中的宠物狗)
{
宠物集名(“Fido”);
}
//或者为每个循环创建一个循环,使您能够知道位置
//当i的整数在循环中递增时,您当前处于arry中
对于(int i=0;i是的,这是可能的。您只需查看一些代码,就可以得到它。+1我开始编写它,但我认为OP在循环中的代码存在进一步的问题。@Yuck-数组选项是有效的,但我不喜欢它,因为其他原因。您好,欢迎您!您介意添加一个代码段和/或解释问题所在吗显示的代码是?
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");
}
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");
}