簡體   English   中英

如何確定字符串變量的值是否在C#中更改?

[英]How do I determine if the value of a string variable changed in 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添加值,之后刪除重復項在我的程序中不起作用。

您是否考慮在私有字段中保留字符串值的副本,並簡單地將新值與舊值進行比較以查看它們是否匹配?

例如:

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

編輯:正如其他答案所示,當然還有其他更復雜的解決方案,比如封裝對屬性中字符串值的所有訪問,或者將字符串包裝在自定義類中。 其中一些替代方案有可能成為“更清潔”,更加面向對象的方法。 但它們比簡單地保存字段中的先前值更復雜。 由您決定您的特定用例是否適合復雜的解決方案,或者更簡單的解決方案。 考慮長期可維護性,而不是現在更容易實現的。

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

如果這個字符串非常重要並且可以傳遞很多,也許你應該把它包裝在一個類中。 該類可以將字符串值保存為屬性,但也可以跟蹤它何時更改。

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

這當然是rudimentery

在頁面加載時,將當前值添加到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..";
}

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

檢查控制值是否更改

我不確定我完全理解,但聽起來你應該使用屬性來設置String x;

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

   get
   {
      return this._x;
   }
}

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

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

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

但是,如果我錯了,你的字符串來自任何其他類型的外部源,要么使用@Ryan Bennett建議的包裝類,要么使用.Net 4,使用動態容器,它會引發PropertyChanged事件任何財產改變時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM