2011-04-08 27 views
3

只有當特定的字符串從其先前的值改變時,我才能在按鈕單擊(將值添加到列表框)下做些事情。我如何管理這個?下面是我的代碼示例:如何確定字符串變量的值是否在C#中更改?

private void button6_Click(object sender, EventArgs e) 
    { 
     string x = //some varying value I get from other parts of my program 
     listBox1.Items.Clear(); 
     listBox1.Items.Add(x + /*other things*/); 
    } 

我可以在次點擊button6時具有相同的值string x從以前的值。在這種情況下,我不希望listBox1添加項目(字符串x)。僅當字符串的值更改時如何添加到列表框?沒有辦法預先確定string x。它在程序運行時獲得價值。

注意:每次向listBox1添加值並稍後刪除重複項不會在我的程序中工作。

+4

請記住,*字符串值永遠不會改變*。字符串是不可變的。 *變量*更改。 – 2011-04-08 14:15:23

+0

button6? ListBox1的?幫你一個忙,給你的表單控件有意義的名字。未來你會感激它。 – asawyer 2011-04-08 14:19:11

+0

@asawyer。是的,當然他們在我的程序中有有意義的名字。在發佈這裏時不會產生很大的變化。這只是我發佈的示例代碼。 – nawfal 2011-04-08 15:10:08

回答

6

你有沒有考慮在私人領域圍繞保持字符串值的副本,只需將新的值進行比較,以舊值,看看它們是否匹配?

例如:

// holds a copy of the previous value for comparison purposes 
private string oldString = string.Empty; 

private void button6_Click(object sender, EventArgs e) 
{ 
    // Get the new string value 
    string newString = //some varying value I get from other parts of my program 

    // Compare the old string to the new one 
    if (oldString != newString) 
    { 
     // The string values are different, so update the ListBox 
     listBox1.Items.Clear(); 
     listBox1.Items.Add(x + /*other things*/); 
    } 

    // Save the new value back into the temporary variable 
    oldString = newString; 
} 

編輯:至於其他的答案表明,一定會有其他更復雜的解決方案,如封裝在屬性中的所有訪問字符串值,或包裹在串一個自定義類。其中一些替代品有可能成爲「更清潔」,更多面向對象的方法。但它們比簡單地將一個字段中的前一個值保存起來更復雜。您需要決定您的特定用例是否適用於複雜的解決方案,或更簡單的解決方案。考慮長期可維護性,而不是現在更容易實施。

+0

我覺得所有其他的答案都應該可以工作,但我有你的工作。謝謝.. – nawfal 2011-04-08 14:07:05

+0

這會以任何方式影響性能嗎? – theGreenCabbage 2014-02-04 15:06:37

3
string last = string.Empty; 
private void button6_Click(object sender, EventArgs e) 
    { 
     string x = //some varying value I get from other parts of my program 
     if(x!=last) 
     { 
      listBox1.Items.Clear(); 
      listBox1.Items.Add(x + /*other things*/); 
      last = x; 
     } 
    } 
2

如果這個字符串是超級重要的並且被很多人接受,也許你應該把它包裝在一個類中。該類可以將字符串值保存爲屬性,但也可以跟蹤它何時發生更改。

public class StringValue 
{ 
    private bool _changed; 
    public string StrValue{get; set{ _changed = true;} 
    public bool Changed{get;set;} 
} 

這當然是rudimentery

+0

這是一個不錯的主意。思考的方式傾向於我 – 2015-04-15 08:04:00

1

我不知道我完全理解,但它聽起來像是你應該使用屬性來設置串x;

string _x = string.Empty; 
public string X 
{ 
    set 
    { 
     if(value != this._x) 
     { 
     DoFancyListBoxWork(); 
     this._x = value; 
     } 
    } 

    get 
    { 
     return this._x; 
    } 
} 
+0

選項是爲DoFancyListBoxWork()使用事件; – sehe 2011-04-08 14:13:54

1

如果這是Web應用程序,請將您的上一個值存儲到會話變量中。如果這是Windows應用程序,請將其存儲在類級變量或單例類中,並使用此最後一個值與新值進行比較。

0

首先,我想問你檢查大多數其他答案。它們更完整,因爲它們處理跟蹤變量變化的更多全球性問題。

現在,我假設,通過閱讀您提供的代碼片段,您需要跟蹤用戶是否更改了字符串因此,換句話說,您可能有一個TextBox或其他類型的控件,用戶可以通過它來更改該值。這是你應該關注的地方:只要使用TextChanged事件。

但是,如果我錯了,並且您的字符串來自任何其他類型的外部源,請使用@Ryan Bennett建議的包裝類,或者如果您使用.Net 4,則使用動態容器,每當任何屬性發生更改時引發一個PropertyChanged事件。

+0

感謝您的加入。不,字符串不會被用戶更改。這是從數據庫中獲得的變量。該字符串可能相同也可能不相同。 – nawfal 2011-04-08 14:43:43

1

在頁面加載時將當前值添加到viewstate,並在按鈕單擊檢查當前值等於視圖狀態中的值。如果兩者都相同,我們可以說價值不變。

protected void Page_Load(object sender, EventArgs e) 
{ 
if (!IsPostBack) 
{ 
    ViewState["CurrentValue"] = Your Value; 
} 
} 
protected void btnSubmit_click(object sender, EventArgs e) 
{ 
if (NewValue== ViewState["CurrentValue"].ToString()) 
{ 
    lblmsg.Text = "value is not changed.."; 
return; 
} 
else 
    lblmsg.Text = "value is changed.."; 
} 

您可以查看該鏈接中的詳細文章。

Check Control Value is changed or not

+0

好吧,我的q是在WinForms上下文,但是,很好的補充。 – nawfal 2014-02-11 08:37:14

相關問題