2011-05-26 301 views
0

基本上我有一個字符串數組,其中包含幾個文本值。我想加載時將數組中的值賦給一個字符串,然後在按鈕上按下它將其更改爲下一個值,一旦它達到最終需要循環。所以字符串將被設置爲數組中的一個值,然後在點擊按鈕後進行更改。如何將一個數組的值添加到字符串中

 Array stringArray = Array.CreateInstance(typeof(String), 3); 
     stringArray.SetValue("ssstring", 0); 
     stringArray.SetValue("sstring", 1); 
     stringArray.SetValue("string", 2); 
+1

需要幫助與學校工作? – Chad 2011-05-26 14:44:15

+0

你可以發佈你到目前爲止 – 2011-05-26 14:46:59

回答

0

設爲首頁= 0

令計數= StringArray.Count

在按鈕點擊執行下列操作

Set Return Value As StringArray(Index) 
Set Index = (Index + 1) Mod Count 

可以在C#是算法程序...

0

你可以有一個持有並迭代你的字符串的類:

class StringIterator 
{ 
    private int _index = 0; 
    private string[] _strings;   

    public StringIterator(string[] strings) 
    { 
     _string = strings; 
    } 

    public string GetString() 
    { 
     string result = _string[_index]; 
     _index = (_index + 1) % _strings.Length; 
     return result; 
    } 
} 

的使用看起來像

class Program 
{ 
    private string _theStringYouWantToSet; 
    private StringIterator _stringIter; 

    public Program() 
    { 
     string[] stringsToLoad = { "a", "b", "c" }; 
     _stringIter = new StringIterator(stringsToLoad); 
     _theStringYouWantToSet = _stringIter.GetString(); 
    }   

    protected void ButtonClickHandler(object sender, EventArgs e) 
    { 
     _theStringYouWantToSet = _stringIter.GetString(); 
    } 

} 
1

下面是一些代碼,讓你去。你沒有提到你使用的是什麼環境(ASP.NET,Winforms等)。

當你提供更多信息時,我會更新我的例子,使它更相關。

public class AClass 
{ 
    private int index = 0; 
    private string[] values = new string[] { "a", "b", "c" }; 

    public void Load() 
    { 
     string currentValue = this.values[this.index]; 
    } 

    private void Increment() 
    { 
     this.index++; 

     if (this.index > this.values.Length - 1) 
      this.index = 0; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Increment(); 
    } 
} 
相關問題