在 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*/);
}
当单击按钮6 时,我有时可以使 string x
的值与之前的值相同。在这种情况下,我不希望 listBox1 添加项目(字符串 x)。如何仅在字符串值更改时添加到列表框?无法预先确定字符串x
。当程序运行时它会获取值。
注意:每次向 listBox1 添加值然后删除重复项在我的程序中不起作用。
I have something to do under a button click (add values to listbox) only if a particular string changes from its previous value. How do I manage this? Below is a sample of my code:
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*/);
}
I can at times have same value for string x
from previous value when clicking button6. In such cases I don't want listBox1 to add the item (string x). How to add to listbox only when value of string changes? There's no way to predetermine string x
. It gets value when program is running.
Note: adding values to listBox1 every single time and later deleting the duplicates wont work in my program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您是否考虑过在私有字段中保留旧字符串值的副本,然后简单地将新值与旧值进行比较以查看它们是否匹配?
例如:
编辑: 正如其他答案所暗示的那样,当然还有其他更复杂的解决方案,例如将对字符串值的所有访问封装在属性中,或者将字符串包装在自定义类中。其中一些替代方案有可能成为“更干净”、更面向对象的方法。但它们都比简单地将先前的值保存在字段中更复杂。由您决定您的特定用例是否值得使用复杂的解决方案或更简单的解决方案。考虑长期的可维护性,而不是现在更容易实施的事情。
Have you considered keeping a copy of the old string value around in a private field, and simply comparing the new value to the old value to see if they match?
For example:
Edit: As the other answers suggest, there are certainly other, more complicated solutions, like encapsulating all access to the string value in a property, or wrapping the string in a custom class. Some of these alternatives have the potential to be "cleaner", more object-oriented approaches. But they're all more complicated than simply saving the previous value in a field. It's up to you to decide whether your specific use case merits the complicated solution, or a simpler one. Think about long-term maintainability, not what's easier for you to implement right now.
如果这个字符串非常重要并且被多次传递,也许你应该将它包装在一个类中。该类可以将字符串值作为属性保存,而且还可以跟踪它何时发生更改。
这当然是初级的
If this string is super important and gets passed around alot, maybe you should wrap it in a class. The class can hold the string value as a property, but also keep track of when it has changed.
this is rudimentery of course
我不确定我是否完全理解,但听起来您应该使用属性来设置 String x;
I'm not sure I understand completely, but it sounds like you should be using a property to set String x;
如果这是 Web 应用程序,请将最后一个值存储到会话变量中。如果这是 Windows 应用程序,请将其存储在类级别变量或单例类中,并使用最后一个值与新值进行比较。
If this is web application, store your last value into session variable. If this is windows application, store it at a class level variable or in singleton class and use this last value for comparison with new value.
在页面加载时将当前值添加到视图状态,然后单击按钮检查当前值是否等于视图状态中的值。如果两者相等,我们可以说该值没有改变。
您可以在此链接中查看详细文章。
检查控件值是否更改
On the page load add the current value to viewstate and at the button click check the current value is equal to the value in the view state. If both are equal we can say that the value is not changed.
You can check the detailed article in this link.
Check Control Value is changed or not
首先,我想请您检查大多数其他答案。它们更完整,因为它们处理跟踪变量变化的更多全局问题。
现在,我假设,通过阅读您提供的代码片段,您需要跟踪用户是否更改了字符串。因此,换句话说,您可能有一个 TextBox 或其他用户可以通过它来更改该值的一种控制。这是您应该集中注意力的地方:只需使用 TextChanged 事件即可。
但是,如果我弄错了,您的字符串来自任何其他类型的外部源,请使用 @Ryan Bennett 建议的包装类,或者如果您使用的是 .Net 4,请使用动态容器,这会引发 PropertyChanged 事件每当任何属性发生更改时。
First, I'd like to ask you to check most of the other answers. They are more complete, in that they treat more global issues of tracking the changes of a variable.
Now, I'm assuming, from reading the snippet of code you provided, that you need to track if a string was changed by the user. So, in other words, you probably have a TextBox or other kind of control through which the user can change that value. This is where you should focus your attention: just consume the TextChanged event.
If, however, I'm mistaken and your string comes from any other kind of external source, either use the wrapper class suggested by @Ryan Bennett or, if you are using .Net 4, use a dynamic container, which raises a PropertyChanged event whenever any property is changed.