2011-12-21 125 views
-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循環:我在變量名

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

我已經嘗試了上面的編碼和它不工作,我只是把它有一個我想要實現的例子

+0

是的,它是可能的。你只需要檢查一些代碼,你就可以得到它。 – DonCallisto 2011-12-21 13:09:46

回答

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這種方法(你可以在函數中初始化,或者作爲一個字段)。

+0

+1我開始寫這篇文章,但我認爲OP在循環中的代碼有更多的問題。 – Yuck 2011-12-21 13:13:15

+1

@Yuck - 數組選項是有效的,但我不喜歡它的其他原因。 – Oded 2011-12-21 13:15:16

+0

@Downvoter - 謹慎評論? – Oded 2011-12-21 13:22:43

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

一個優勢,使用這種方法,您可以添加在飛行中更多的控制,甚至編程,如果你保持軌道你需要處理的子網數量,你不必更新你的代碼。

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

你好,歡迎光臨!你介意添加一段代碼和/或解釋顯示代碼的問題是什麼? – 2016-02-01 14:53:36