如何将布尔值从内部包含类冒泡到外部包含类

发布于 2024-12-05 05:21:34 字数 481 浏览 0 评论 0原文

我有 3 个类:

class A 
{
    public B b = new B();    
    public bool flag {get; set;}
}

class B
{
   piblic C c = new C();
}


class C
{
   public void foo()
    {
  //iterates a dataTable with column "someBoolCondition"
  // I want to set A's bool to true, after the first record that has 'true' in column
  //"someBoolCondition". Thus is thought to avoid bool memebers in each class.
    }
}

从 C 的 foo 将 A 的标志设置为“true”的最佳方法是什么?

TIA

I have 3 classes:

class A 
{
    public B b = new B();    
    public bool flag {get; set;}
}

class B
{
   piblic C c = new C();
}


class C
{
   public void foo()
    {
  //iterates a dataTable with column "someBoolCondition"
  // I want to set A's bool to true, after the first record that has 'true' in column
  //"someBoolCondition". Thus is thought to avoid bool memebers in each class.
    }
}

What it the best way set A's flag to 'true' from C's foo?

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情 2024-12-12 05:21:34

每次 C 更改自己的布尔值时,您的 C 都可以触发一个事件。您的 A 可以订阅事件并自行更新。您还可以将 A 的抽象接口传递给 b 和 C,让他们直接推送更改。

Your C can fire an event every time C changes its own bool. Your A can subscribe to event and update itself. You can also pass an abstracted interface of A to b and C for letting them push the change directly.

極樂鬼 2024-12-12 05:21:34

另一个解决方案也是最简单的形式架构观点,但我不清楚在您的具体情况下是否可以接受,就是在 A 类中拥有一个 static 属性。

public class A 
{
     private static bool failed = false; 
     public static bool Failed {get {return failed;} set {failed=value;}}
}

以及 foo() 函数代码中的某个位置:

foo(...) 
{
    //failure happens! 
    A.Failed = true;
}

这很简单且清晰,但这取决于从应用程序架构的角度来看它是否可以接受。

希望这有帮助。

Another solution again, simplest form architectual point of view, but it's not clear to me if it's acceptable in your specific case, is just to have a static property in A class.

public class A 
{
     private static bool failed = false; 
     public static bool Failed {get {return failed;} set {failed=value;}}
}

and somewhere in the code in your foo() function:

foo(...) 
{
    //failure happens! 
    A.Failed = true;
}

This is easy and clear, but it depends if it's acceptable from you app architectural point of view.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文