简体   繁体   English

C# - 如何检查字符串值是否在不同的类中发生了变化?

[英]C# - how to check if string value has changed within a different class?

I have a class as follows:我有一堂课如下:

class TestSomeData
{
    public string someString;
    public void CheckSomeStuff()
    {
         foreach(string x in someList)
         {
              someString = x;
          }
     }
 }

What i want to do is this:我想做的是:

TestSomeData test = new TestSomeData;
test.CheckSomeStuff();
label1.Content = test.someString;

So if x is 1,2,3,4,5 out of someList in CheckSomeStuff, i need the label1.Content to reflect that, but from a different class.因此,如果 x 是 CheckSomeStuff 中 someList 中的 1、2、3、4、5,我需要 label1.Content 来反映这一点,但来自不同的类。 how do i do that?我怎么做?

A class can inform other classes of changes made to it by declaring events一个类可以通过声明事件来通知其他类对其所做的更改

public class SomeDataClass
{
    public event EventHandler SomeDataChanged;

    private string _someData;
    public string SomeData
    {
        get { return _someData; }
        set
        {
            if (value != _someData) {
                _someData = value;
                OnSomeDataChanged();
            }
        }
    }

    private void OnSomeDataChanged()
    {
        SomeDataChanged?.Invoke(this, EventArgs.Empty);
    }
}

class InterestedClass
{
    public void Work()
    {
        var someData = new SomeDataClass();
        someData.SomeDataChanged += someData_SomeDataChanged;
        someData.SomeData = "New Value";
    }

    void someData_SomeDataChanged(object sender, EventArgs e)
    {
        Console.WriteLine("Some data changed: {0}", ((SomeDataClass)sender).SomeData);
    }
}

You could also make the event static.您还可以将事件设为静态。 This would allow a listener to listen to changes of any instance of this class.这将允许侦听器侦听此类的任何实例的更改。

SomeDataClass.SomeDataChanged += SomeDataClass_SomeDataChanged;

var someData = new SomeDataClass();
someData.SomeData = "New Value";
var someOtherData = new SomeDataClass();
someData.SomeData = "Other Value";

You could pass a reference to the control so that you can change its text directly.您可以传递对控件的引用,以便您可以直接更改其文本。 Not saying this design is particularly pretty but it would get the job done.并不是说这个设计特别漂亮,但它可以完成工作。

class TestSomeData
{
    public TestSomeData(Label myControl) {
        this.MyControl = myControl;
    }

    private Label MyControl { get; set; }

    public void CheckSomeStuff()
    {
         if (this.MyControl == null) {
             // throw ArgumentNullException or InvalidOperationException
         }

         foreach(string x in someList)
         {
              this.MyControl.Content = x;
         }
     }
 }

Also as others have noted, the control will always have the last value found in the list.同样正如其他人所指出的,控件将始终具有列表中的最后一个值。 If you're trying to have the label display each value as the list enumerates you're going to have to add some UI logic to force the control to refresh or repaint.如果您试图让标签在列表枚举时显示每个值,您将不得不添加一些 UI 逻辑来强制控件刷新或重新绘制。

You should make that field static .您应该将该字段设为 static All instances of that class will have that something in common.该类的所有实例都将具有共同点。 Any change will be globally.任何更改都将是全球性的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM